1

I am creating one app In which I want to check status of activity like activity is in foreground or in background.this is working perfect from code but I want to bring activity to foreground when it is in background for that I am using service so when activity is going to background state I am calling that service and from that service I am launching activity but service is working ok upto version 10 after that same code of intent is not working. This is my code. MainActivity.Java

 @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            if(android.os.Build.VERSION.SDK_INT>=29 && 
    !Settings.canDrawOverlays(getApplicationContext())) {
                MainActivity.this.startActivity(new 
    Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION));
            }

        }

This is DigitalApp class which extend application and from this I am calling service.

        public class DigitalApp extends Application implements Application.ActivityLifecycleCallbacks 
    {

        private int activityReferences = 0;
        private boolean isActivityChangingConfigurations = false;

        @Override
        public void onCreate() {
            super.onCreate();

            registerActivityLifecycleCallbacks(this);

        }

        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

        }

        @Override
        public void onActivityStarted(Activity activity) {
            if (++activityReferences == 1 && !isActivityChangingConfigurations) {
                // App enters foreground

                Toast.makeText(this,"App is in foreground with 
    activity::"+activity.getLocalClassName(),Toast.LENGTH_LONG).show();
                Log.d("Tracking Activity Started", activity.getLocalClassName());

                Log.d("TAG","App is in foreground");
            }

        }

        @Override
        public void onActivityResumed(Activity activity) {

        }

        @Override
        public void onActivityPaused(Activity activity) {

        }

        @Override
        public void onActivityStopped(Activity activity) {

           Toast.makeText(this,"App enters background with 
    activity::"+activity.getLocalClassName(),Toast.LENGTH_LONG).show();

            isActivityChangingConfigurations = activity.isChangingConfigurations();
            if (--activityReferences == 0 && !isActivityChangingConfigurations) {
                // App enters background

                Log.d("Tracking Activity Stopped", activity.getLocalClassName());
                Log.d("TAG","App enters background");
                Intent i= null;



                   getApplicationContext().startService(new 
    Intent(getApplicationContext(),service.class));



            }
        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

        }

        @Override
        public void onActivityDestroyed(Activity activity) {

        }
    }

This is Service class.

         @Override
        public void onCreate() {
            super.onCreate();


            Handler handler = new Handler();

            handler.postDelayed(new Runnable() {
                public void run() {

                    Toast.makeText(getApplicationContext(), "IN HANDLER OF SERVICE", 
    Toast.LENGTH_LONG).show();


                    Intent i = new Intent(getApplicationContext(), MainActivity.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    getApplicationContext().startActivity(i);




            }, 3000);


        }

I am passing intent in handler to launch activity when it is going to background state.handler is in oncreate method of service.I want launch activity from there again after it is going to background state.

I am using flag new task but with this flag activity is not launching will I add other flags?

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • just to clarify, are you trying to launch application, after a delay, when it is closed by user? – Makarand Sep 19 '22 at 10:09
  • yes After 3 seconds I want to open that application again – Nidhi Bhatt Sep 19 '22 at 10:13
  • Do you know if your `Service` is a actually getting started? You might want to add some debug logging so you can see what is going on. – David Wasser Sep 19 '22 at 17:34
  • Yes service is started I got toast message which I print in service on start and on create method.and also ACTIVITY is relaunching in android 10 version so service is starting. – Nidhi Bhatt Sep 20 '22 at 04:44
  • Have you implemented `onStartCommand()` in the `Service`? – David Wasser Nov 15 '22 at 09:39
  • Do you know if the `run()` method that you pass to the `Handler` is being executed? Using `Toast` is not a good way to validate this. Better would be to add logging. – David Wasser Nov 15 '22 at 09:40

1 Answers1

0

from the onStop() of your activity start the service like this-

fun launchService(){
    val serviceIntent = Intent(this, LaunchAppService::class.java)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startForegroundService(serviceIntent)
    }else{
        startService(serviceIntent)
    }
}

and show notification for foreground service like this

Makarand
  • 983
  • 9
  • 27