Android - How to work with Services?
The first time my app loads it starts my main activity, and inside my main a开发者_如何学运维ctivity i automatically start a service:
Intent s = new Intent(this, Soc.class);
startService(s);
//start the service for the first time
I need to make sure that when the user is opening the app NEXT TIME, it kills the old service, and recreate the service:
@Override
//this code is on every activity in my application
protected void onRestart()
{
super.onRestart();
Intent s = new Intent(this, Soc.class);
stopService(s);
//kill the service
startService(s);
//start a brand new service
}
Is this the correct way of killing a service and renewing the service?
Is this the correct way of killing a service and renewing the service?
Well, it might work, but it is rather wasteful. Why are you bothering with a service in this case? Or, conversely, why do you have to destroy and recreate the service just to update it?
精彩评论