0

I have an app that needs to run 24/7. I know that android OS kills the process of my app after some time in the background, so i have a Foreground service which is sticky. Now the thing is, that i want the service to check if my app is running, and if it is not, then i want the service to start the app. Is it possible?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ohados
  • 83
  • 3
  • 13

2 Answers2

1

After onStop your activity will be invisible. If you want to see that your activity running, i.e., on the foreground;

public boolean isForeground(String myPackage) {
    ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> runningTaskInfo = manager.getRunningTasks(1); 
    ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
    return componentInfo.getPackageName().equals(myPackage);
}

If it is not in the foreground, you can use below otherwise;

Intent i = new Intent(this, MyMainActivity.class);
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
  • Note; another method, you can send message to your service from your activity about its status
  • or, use idolon's idea]2;
kelalaka
  • 5,064
  • 5
  • 27
  • 44
  • Yea, but how can the Foreground service check if the app is running or not? – Ohados Sep 25 '18 at 12:33
  • But if it is running, it wont just "bring it back", it will call it's onCreate() and will create a new instance of it. – Ohados Sep 25 '18 at 13:12
  • So that's exactly what im asking...how can i know if the app is running or not ? – Ohados Sep 25 '18 at 13:14
  • if it is in the foreground it is running! use the isForeground boolean function. – kelalaka Sep 25 '18 at 13:16
  • 1
    Eventually what i did was using this code: `Intent i = new Intent(this, MyMainActivity.class); i.setAction(Intent.ACTION_MAIN); i.addCategory(Intent.CATEGORY_LAUNCHER); startActivity(i);` In the foreground service onCreate(). Now if android will kill my process, the service will restart and it will bring up the application. – Ohados Sep 25 '18 at 18:25
0

What kind of app is it? I think such types of apps can be very annoying for users. I am not sure what exactly functionality your app have, but are you sure that you can't achieve that same functionality with using AlarmManger or JobScheduler ?

Vito
  • 1,414
  • 1
  • 13
  • 22
  • 1
    What do you mean by very annoying for users? is whats'app also very annoying because it is running 24/7? The app is for private use only and this is the request.. – Ohados Sep 25 '18 at 15:35
  • @Ohados , if it's some special case, maybe it's fine, I am just saying that usual Android user can be mad on such types of apps, that running all the time and users will see that your app wastes a lot of battery and will remove your app, so my point is does your app really need to be alive all the time? – Vito Sep 28 '18 at 11:06
  • 1
    There are can be a lot of different apps that may require it. For example an app for taxi drivers. – Fernando Luiz Jan 16 '20 at 17:09