Android: Create service that runs when application stops

11,168

I wouldn't use the android:process attribute - this actually runs your service in a separate process and makes it hard to do things like share preferences. You won't have to worry about your service dying when your application dies, the service will keep going (that is the point of a service). You also don't want a binding service because that will start and die when the bindings do. That being said:

<service
    android:enabled="true"
    android:exported="true"
    android:name=".MyService"
    android:label="@string/my_service_label"
    android:description="@string/my_service_description"
    <intent-filter>
        <action android:name="com.package.name.START_SERVICE" />
    </intent-filter>
</service>

You can define your own action for the intent to launch the service (cannot be launched by the system - has to be an activity). I like to set "enabled" to true so the system can instantiate my service, and "exported" so other applications can send intents and interact with my service. You can find more about this here.

You can also make a long running service that uses bindings, if you do this just add and intent for the binder such as:

<action android:name="com.package.name.IRemoteConnection" />

Now to start the service from your activity:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent serviceIntent = new Intent("com.package.name.START_SERVICE");
        this.startService(serviceIntent);

        // You can finish your activity here or keep going...
    }
}

Now for the service:

public class MyService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        // This is triggered by the binder action intent.
        // You can return your binder here if you want...
        return null;
    }


    @Override
    public void onCreate() {
        // Called when your service is first created.
    }

    @Override
    public in onStartCommand(Intent intent, int flags, int startId) {
        // This is triggered by the service action start intent
        return super.onStartCommand(intent, flags, startId);
    }
}

Now your service should run. To help with the longevity there are some trick. Make it run as a foreground service (you do this in the service) - this will make you create a notification icon that sticks in the status bar. Also keep your HEAP light to make you application less likely to be killed by Android.

Also the service is killed when you kill the DVM - thus if you are going to Settings->Applications and stopping the application you are killing the DVM and thus killing the service. Just because you see your application running in the settings does no mean the Activity is running. The activity and service have different life-cycles but can share a DVM. Just keep in mind you don't have to kill your activity if not needed, just let Android handle it.

Hope this helps.

Share:
11,168

Related videos on Youtube

zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
Author by

zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

ADO.NET, MS SQL SERVER, Windows Forms Applications(VB and C#), Visual FoxPro, ASP.NET, WCF Web Services, Windows Mobile, and Android 2.2 &amp; 3.0. When you sort all of Stackoverflow's members alphabetically, I am last. Check out my Android calculator. It can perform calculations in any numeral system from base 2 to base 36. It is free.Any Base Calculator Add me on google plus and I will add you into my developers circle.

Updated on June 04, 2022

Comments

  • zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
    zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz almost 2 years

    How do you create a Service that keeps on running when your application is stopped? I am creating the service in a separate process in the manifest file:

    <service
        android:name="com.example.myservice"
        android:process=":remoteservice" />
    

    I am also returning START_STICKY in the Service's onStartCommand:

    @Override
    public in onStartCommand(Intent intent, int flags, int startId){
        return Service.START_STICKY;
    }
    
    • Mixer
      Mixer over 9 years
      I have the same problem. Did you find a solution?
  • zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
    zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz about 12 years
    I am 100% sure my service dies when my application dies. When my application is running i see it under settings-->Applications-->Running Services. When I kill my application the service is no longer there.
  • jjNford
    jjNford about 12 years
    Try running it as a foreground service and set the enabled attribute so the service will be instantiated with a system context rather than application context.
  • zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
    zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz about 12 years
    I set enabled to true and made it a foreground service, I think the issue is I am starting it in the application context.
  • jjNford
    jjNford about 12 years
    Then setting the enabled and starting the service with an intent as shown above should fix this. It works perfect for me - using the tips I have shown my services stay alive for days...
  • jjNford
    jjNford about 12 years
    I even kill off my activity after the service starts using the onFinish() method. So I know this works.
  • jjNford
    jjNford about 12 years
    The intent you define in the action category of your manifest for the services intent filter. You can create any intent you like (ex: "HelLo.WooorLD.GO" would work just fine as long as that is the intent you use when calling your service to start)
  • zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
    zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz about 12 years
    I have done all these things and my service is still killed when my application is killed.
  • jjNford
    jjNford about 12 years
    How is you application being killed? What are you doing to kill it?
  • zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
    zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz about 12 years
    I am going to settings-->applications-->manage applications-->my application-->force stop
  • jjNford
    jjNford about 12 years
    .. I mentioned this above. This is not killing your application in terms of the activity - this is killing the entire Virtual Machine that your activities and service run in. That should kill your service. This is like running a "kill -9 pid" from the shell. Android does not do this to your application. Android will call you applications onDestroy() or onFinish() method, so you don't need to worry about this.
  • zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
    zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz about 12 years
    I need the service to keep running when the Virtual Machine is killed.
  • jjNford
    jjNford about 12 years
    To make sure your service runs when the "application" dies just destroy your activities using this.onFinish(). Again killing your application through the settings causes a System.exit() which will kill the entire Dalvik virtual Machine including the service... you will never never have to plan for this...
  • jjNford
    jjNford about 12 years
    the application is not being killed when you do this... the virtual machine is. there is no way around this. you will not have to worry about this.
  • zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
    zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz about 12 years
    I need the service to always be running even when the user kills the virtual machine. I have seen other applications do this with services such as the ESPN Scorecenter application. It runs a background service even after I kill the virtual machine in order to push notifications to the phone.
  • jjNford
    jjNford about 12 years
    Then they are just running the service in a remote process... remote process = different virtual machine
  • zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
    zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz about 12 years
    Even when I run the service in a separate process it is still being stopped when the virtual machine is stopped. Maybe I need to create the process differently. My other thought is to use the alarm manager to start up the service again if it has been stopped.
  • jjNford
    jjNford about 12 years
    Yea... you might also checkout the ACTION_INTENTS thrown by the system (look in the API). Maybe something there will help. I have never had a problem. However I do remember there was something special you had to do for a remote process service than what the API says... you may try searching for that also... Sorry couldn't help more.