1

Problem statement: I want to launch an application which contains a set of services [run in the background]. These services will be communicating to a client activity [which also is a part of the application]. When the application is killed (manually by swiping out the running application OR through Settings->Apps->Running application->stop the application), I want the services to be running in the background. When the application is launched back, it will resume the communication with the services running in the background.

Solution:

For the communication between the services and the client activity, I have put all the background services in a separate process and the activity in the application process. The services and the activity communicate through the Messenger (the IPC provided by android). This works fine, without any issue.

The problem I am having is with the services to be run in the background when the application which launched the services is killed manually.

Killing manually implies: manually by swiping out the running application OR through Settings->Apps->Running application->stop the application.

For this I tried the following

  1. The set of services are running as a global process (by using android:process in the Manifest). But these services are killed when the application is killed.

  2. I include 2 activities in the Manifest file and both have following lines in the Manifest

"android.intent.action.MAIN" and "android.intent.category.LAUNCHER" for both the activities.

Here out of the 2 activities, one activity and all the services are made a part of one process. The other activity (which is the client activity that uses the services) is made a part of the application. Basically thinking that 2 applications will be launched and one of the application (which holds all the services) will be running irrespective of the other being killed. However I see that only one application will be running and when I kill it, all the services are made to stop.

So I think that the only way I could possibly achieve this is, if I am able to launch 2 Virtual Machines from a single apk. So that all the services are part of one VM and the client activity(that uses the services) is part of the other VM. Now if the application holding the client activity is killed, the VM holding the services will be running without any issue.

But is there is a way that one apk can allow for launching 2 applications which run on 2 different VMs. If yes, how can I do that?

OR

Is there any other solution to my problem statement.

Thanks in advance.

APD
  • 23
  • 5
  • 1
    I think you need to plan for your service process to be shut down. Allowing a process to continue running when the user explicitly stops it would be a major design flaw (and a potential security risk if the app is malicious). – Karakuri Dec 18 '15 at 16:44
  • The default state for every app should be not running anything. What are you doing that you want a persistent service? – ianhanniballake Dec 18 '15 at 17:04
  • Thanks Karakuri and ianhanniballake for the answer. My background services are very light and will be waiting for the client activity command for an action to be performed. This is something I need by design. The client activity maybe killed or restarted at various time instants, but services need to be doing something based on client's command. – APD Dec 19 '15 at 07:04
  • You don't need the service running all the time just to wait for a client command. That's what `startService()` is for. If the client Activity is gone (because the user stopped the app or the system terminated it), then there's no point in the service running any more anyway as nothing will be sending it commands. – Karakuri Dec 29 '15 at 23:59

2 Answers2

2

You can certainly restart the service when the app is killed. You just have to handle the uncaughtException and fire the alarm that starts the service again. Follow my post to know more around restarting services.

Community
  • 1
  • 1
cgr
  • 4,578
  • 2
  • 28
  • 52
  • @APD, be aware this code won't restart the service if app is forcefully speed by user through settings. As user doesn't want app running, or makes sense. – cgr Dec 19 '15 at 07:42
1

There is a better solution for this. Yes when user kill application all services will be stopped. So you have to restart the service.

For this there is a simple technique.

Create a receiver which check if service is running or not, if not start the service. and after this create a entry on alarmanager to recall this reciever after 2 sec, so after every 2 sec it check whether your service is running or not.

Please check below code.

    public class MyServiceReceiver extends BroadcastReceiver {
    Context con;

    @Override
    public void onReceive(final Context context, final Intent intent) {
        this.con = context;
        if (!Support.isServiceRunning(context, MyService.class.getName())) {
                con.startService(new Intent(con, MyService.class));
        }        
        Intent in = new Intent(con, MyServiceReceiver.class);
        PendingIntent pi = PendingIntent.getBroadcast(con, 0, in, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager alarmmanager = (AlarmManager) con.getSystemService(Context.ALARM_SERVICE);
        alarmmanager.set(AlarmManager.RTC, System.currentTimeMillis() + 2000, pi);
    }
}
Deepak
  • 756
  • 4
  • 10
  • Well I did it, and its not causing any issue.. I have decompile many apps they are using same technique to run their background service always. Perhaps you can increase your time from 2 second to any suitable value. But thats the only possible way. I found – Deepak Dec 18 '15 at 16:44
  • Not causing any issue _that you know of_. What about the user's battery life? Consider what they see when they look at the list of apps that are running and see your app at the top because it's keeping the CPU active. Then they really will want to kill your app. – Karakuri Dec 18 '15 at 16:56
  • Well I tried that too.. its not coming on top as other app also doing the same thing. in my case its coming on 10th rank above that apps like Facebook, Whatsapp, Hike are there – Deepak Dec 19 '15 at 04:23
  • Thanks Trush, I believe the approach is similar to that of cgr's. Will try that and will reply back. – APD Dec 19 '15 at 07:10
  • @Karakuri what's your approach to this problem? Can you add any solution you know that is win-win? – sirvon Dec 29 '15 at 22:55