Android : Android service thread lifecyle and intent

on Wednesday, April 1, 2015


So the first part of my question regards the lifecycle of threads started inside a service. My reason behind starting these threads is to give them a better chance of staying alive outside of my application. Will these threads started inside a service actually inherit the service priority? (My use case here is to have a thread that can run indefinitely and is continuously waiting for work to do)


From The Android Dev Site:



Because a process running a service is ranked higher than a process with background activities, an activity that initiates a long-running operation might do well to start a service for that operation, rather than simply create a worker thread—particularly if the operation will likely outlast the activity.



Next deals with the shutdown of the service. Lets say we pass in some information as part of the service intent.



@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
m_Bundle = intent.getBundleExtra("bundle");
return START_STICKY;
}


From what I understand, if the the service is restarted by the system, START_STICKY will start with a null intent, and I will lose my bundle. Is there any way to persist this so I still have that information if the service is restarted?


0 comments:

Post a Comment