如何在Android 10中从后台启动活动?

编程入门 行业动态 更新时间:2024-10-07 10:25:45
本文介绍了如何在Android 10中从后台启动活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在构建一个Android应用,该应用需要从后台开始活动.我正在使用扩展Service的ForegroundStarter来完成此任务.我有一个活动Adscreen.class,需要从我的前台服务中运行.活动Adscreen.class在除Android 10以外的所有Android版本上都可以正常运行(从后台开始).

ForeGroundStarter.class

public class ForeGroundStarter extends Service { @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); Log.d("sK", "Inside Foreground"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d("sK", "Inside Foreground onStartCommand"); Intent notificationIntent = new Intent(this, Adscreen.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); Notification notification = null; //Launching Foreground Services From API 26+ notificationIntent = new Intent(this, Adscreen.class); pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { String NOTIFICATION_CHANNEL_ID = "com.currency.usdtoinr"; String channelName = "My Background Service"; NotificationChannel chan = null; chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE); chan.setLightColor(Color.BLUE); chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); assert manager != null; manager.createNotificationChannel(chan); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID); notification = notificationBuilder.setOngoing(true) .setSmallIcon(R.drawable.nicon) .setContentTitle("") .setPriority(NotificationManager.IMPORTANCE_MIN) .setCategory(Notification.CATEGORY_SERVICE) .build(); startForeground(2, notification); Intent dialogIntent = new Intent(this, Adscreen.class); dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(dialogIntent); Log.d("sk", "After startforeground executed"); } else //API 26 and lower { notificationIntent = new Intent(this, Adscreen.class); pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification = new Notification.Builder(this) .setContentTitle("") .setContentText("") .setSmallIcon(R.drawable.nicon) .setContentIntent(pendingIntent) .setTicker("") .build(); startForeground(2, notification); Intent dialogIntent = new Intent(this, Adscreen.class); dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(dialogIntent); } return super.onStartCommand(intent, flags, startId); } }

我阅读到从Android 10的后台启动活动有一些限制.此代码似乎不再起作用. developer.android/guide/components/activities/background-starts

Intent dialogIntent = new Intent(this, Adscreen.class); dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(dialogIntent);

有什么变通办法可以在Android 10上从后台启动活动吗?

解决方案

不确定这样做是否正确,但是我没有足够的时间(该应用仅供公司内部使用).

我使用了权限:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

然后每个用户都必须转到应用程序的设置->权限,然后在高级设置中选中功能​​将应用程序展示给其他用户"

很抱歉我的英语不是完全正确的解决方案,但是它确实有效,所以对我来说是很好的修复程序.

在Pixel 4上,它将如下所示:

I am building an android app where I need to start an activity from background. I am using a ForegroundStarter which extends Service for accomplishing this. I have an activity Adscreen.class which I need to run from my Foreground service. The activity Adscreen.class works fine(starts from background) on all Android versions except Android 10.

ForeGroundStarter.class

public class ForeGroundStarter extends Service { @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); Log.d("sK", "Inside Foreground"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d("sK", "Inside Foreground onStartCommand"); Intent notificationIntent = new Intent(this, Adscreen.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); Notification notification = null; //Launching Foreground Services From API 26+ notificationIntent = new Intent(this, Adscreen.class); pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { String NOTIFICATION_CHANNEL_ID = "com.currency.usdtoinr"; String channelName = "My Background Service"; NotificationChannel chan = null; chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE); chan.setLightColor(Color.BLUE); chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); assert manager != null; manager.createNotificationChannel(chan); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID); notification = notificationBuilder.setOngoing(true) .setSmallIcon(R.drawable.nicon) .setContentTitle("") .setPriority(NotificationManager.IMPORTANCE_MIN) .setCategory(Notification.CATEGORY_SERVICE) .build(); startForeground(2, notification); Intent dialogIntent = new Intent(this, Adscreen.class); dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(dialogIntent); Log.d("sk", "After startforeground executed"); } else //API 26 and lower { notificationIntent = new Intent(this, Adscreen.class); pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification = new Notification.Builder(this) .setContentTitle("") .setContentText("") .setSmallIcon(R.drawable.nicon) .setContentIntent(pendingIntent) .setTicker("") .build(); startForeground(2, notification); Intent dialogIntent = new Intent(this, Adscreen.class); dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(dialogIntent); } return super.onStartCommand(intent, flags, startId); } }

I read that there are some restrictions on starting activities from background on Android 10. This code doesnt seem to be working anymore. developer.android/guide/components/activities/background-starts

Intent dialogIntent = new Intent(this, Adscreen.class); dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(dialogIntent);

Any workarounds to start an activity from background on Android 10?

解决方案

Not sure if it's right to do it this way, but I did not have enough time (app is only for company internal use).

I used permissions:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

and then every user has to go to setting -> permissions of the app and then check box in advanced settings for function "show the app over others"

Sorry for my English or not exactly the right solution, but it worked, so good hotfix for me.

On Pixel 4 it will be looking like this:

更多推荐

如何在Android 10中从后台启动活动?

本文发布于:2023-11-05 10:24:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560576.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:后台   如何在   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!