Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

foreground service - How To Start An Activity From Background in Android 10?

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. 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);

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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: enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...