0

I have been searching around for a way to kill my application after the user put it in the background for at least 30min.

So far what i have found things like :

getActivity().finish();
Process.killProcess(Process.myPid());
System.exit(1);

private void stopServices() {        
    final ActivityManager activityManager = SystemServices.getActivityManager(context);
    final List<ActivityManager.RunningServiceInfo> runningServices = activityManager.getRunningServices(Integer.MAX_VALUE);
    final int pid = Process.myPid();
    for (ActivityManager.RunningServiceInfo serviceInfo : runningServices) {
        if (serviceInfo.pid == pid && !SenderService.class.getName().equals(serviceInfo.service.getClassName())) {
            try {
                final Intent intent = new Intent();
                intent.setComponent(serviceInfo.service);
                context.stopService(intent);
            } catch (SecurityException e) { 
                 // handle exception
            }
        }
    }
}

Pretty much what i have figered so far is taht after API 21 use

finishAndRemoveTask();

before that use this.finishAffinity();

But how to start timer once the app is put in the background for X time and then call the app kill method.

Hossam Hassan
  • 795
  • 2
  • 13
  • 39

1 Answers1

0

So what I understand that you need 2 things:

  1. Figure out when your app is in the background
  2. Schedule a job.

So here are 2 links for this questions:

  1. How to detect if your app is in background
  2. Schedule a job on Android
Uriel Frankel
  • 14,304
  • 8
  • 47
  • 69
  • Ya thats what i have found out from search that i should use AppLifeCycleHandler and with it i can get when the app in background or foreground but now i have to start service once the app is in background and be able to cancel that specific service if the app switch to be in foreground – Hossam Hassan Dec 03 '18 at 15:46