Search code examples
androidserviceprocesslifecycle

Android: Create service that runs when application stops


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;
}

Solution

  • 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.