开发者

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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜