android - 如何在 Android 10 中从后台启动 Activity?

标签 android foreground-service

我正在构建一个需要从后台启动 Activity 的 android 应用程序。我正在使用 ForegroundStarter 扩展服务来完成此操作。我有一个 Activity Adscreen.class 我需要从我的前台服务运行。 Activity 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 上从后台启动 Activity 有一些限制。这段代码似乎不再有效。
https://developer.android.com/guide/components/activities/background-starts
Intent dialogIntent = new Intent(this, Adscreen.class);
            dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(dialogIntent);

在 Android 10 上从后台启动 Activity 的任何解决方法?

最佳答案

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

我使用了权限:

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

然后每个用户都必须转到设置->应用程序的权限,然后在高级设置中选中功能“​​将应用程序显示在其他人之上”的复选框

对不起我的英语或不完全正确的解决方案,但它有效,对我来说很好的修补程序。

在 Pixel 4 上,它看起来像这样:
enter image description here

关于android - 如何在 Android 10 中从后台启动 Activity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58245398/

相关文章:

java - 如何在android中将TextView转换成ImageView

Android WebView 网页不适合整个屏幕????

android - android中第二次观察viewmodel返回null

java - startForegroundService() 没有调用 startForeground(),但它调用了

java - 杀死并重新启动应用程序后前台服务未停止 - Android Studio

android - Intent getStringExtra 返回 null

android - 为什么我的单元测试针对的是错误版本的Android?

android - 小米设备停止前台服务

Android - 如何为 API-28 使用 startForegroundService() 和 startForeground()?

android - 如何更新自定义布局通知操作?