I'm reading through the docs and the more time I spend I get more confused what's the easiest way to accomplish what I'm trying to do. I want to write a simple Service, which starts at button onClick and binds to the activity. And when the activity is closed and started again later (not only restarted!), I want to check whether the service is already running and bind to it. How do I do it?
Thanks
"If you start an android Service with startService(..) that Service will remain running until you explicitly invoke stopService(..). There are two reasons that a service can be run by the system. If someone calls Context.startService() then the system will retrieve the service (creating it and calling its onCreate() method if needed) and then call its onStartCommand(Intent, int, int) method with the arguments supplied by the client. The service will at this point continue running until Context.stopService() or stopSelf() is called. Note that multiple calls to Context.startService() do not nest (though they do result in multiple corresponding calls to onStartCommand()), so no matter how many times it is started a service will be stopped once Context.stopService() or stopSelf() is called; however, services can use their stopSelf(int) method to ensure the service is not stopped until started intents have been processed.
Clients can also use Context.bindService() to obtain a persistent connection to a service. This likewise creates the service if it is not already running (calling onCreate() while doing so), but does not call onStartCommand(). The client will receive the IBinder object that the service returns from its onBind(Intent) method, allowing the client to then make calls back to the service. The service will remain running as long as the connection is established (whether or not the client retains a reference on the service's IBinder). Usually the IBinder returned is for a complex interface that has been written in aidl.
A service can be both started and have connections bound to it. In such a case, the system will keep the service running as long as either it is started or there are one or more connections to it with the Context.BIND_AUTO_CREATE flag. Once neither of these situations hold, the service's onDestroy() method is called and the service is effectively terminated. All cleanup (stopping threads, unregistering receivers) should be complete upon returning from onDestroy()."
you can check if service is running or not:
public boolean isServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("com.example.app.ServiceClassName".equals(service.service.getClassName())) {
return true;
}
}
return false;
}
In that Scenario I think you only need to use AsyncTask
hope this helps
To perform actual binding of services to activities in android, you'll need to extend the Service class and bind the service to your activity ( and possibly use Android's AIDL to perform communication between the activity & service). However, in your example, is sounds like you could get away with just using a separate thread within your activity. Use either a runnable or an AsyncTask. Once the user clicks the button that fire's the onClick, set a static boolean within your application to true. Then, upon re-entry into the activity, simply check that boolean for true and, if true, fire off the thread again.