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;
}
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:
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:
Now to start the service from your activity:
Now for the service:
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.