为什么服务中的变量没有“重置”?停止服务后(其 onDestroy 已执行)?
在一个活动中,我定义了以下按钮侦听器:
final Button button = (Button) findViewById(R.id.buttonExit);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
stopService(new Intent(StartupActivity.this, SamcomService.class));
finish();
}
});
如您所见,按钮应该停止正在运行的服务(在上一步中创建),然后完成自身。
当我按下按钮时,Service.onDestroy 将按预期执行。在 onDestroy 中我做了一些清理,然后最后调用 super.onDestroy():
@Override
public void onDestroy() { // the service onDestroy
// Do some cleaning
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(1);
// more cleaning
Toast.makeText(this, "The service has been stopped! Wii!", Toast.LENGTH_LONG).show();
super.onDestroy();
}
我的世界,这意味着这个 Service 已经死亡并被埋葬,连同其中的所有变量。正确的? 嗯,看起来不像。
我的想法是,我的服务中有一个字符串,在单击按钮停止服务之前,我会在其中附加文本。像这样的事情:
public class SamcomService extends Service {
private String startupText = "";
private void addTextToStartup(String text)
{
startupText += text;
// Sending a broadcast, not relevant
// ...
}
// ...
}
当我再次启动我的应用程序时,该字符串 startupText 不会重置!就像服务根本没有被杀死一样。 startupText 包含在上次运行中添加到其中的所有文本。
这是为什么?我缺少什么?服务不是已经死了吗?当我再次启动应用程序时,将调用 Service onCreate 方法,这意味着它是从头开始的......
--- 编辑 ---
我刚刚读到了这个: onDestroy() 到底销毁了什么?
这意味着 onDestroy 并没有真正销毁任何东西。正确的?它非常蹩脚,而且非常烦人。我想在两年前创建的一个访问量很大的帖子讨论了这个问题......: 退出应用程序会让人皱眉吗?
In one Activity, I define the following Button listener:
final Button button = (Button) findViewById(R.id.buttonExit);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
stopService(new Intent(StartupActivity.this, SamcomService.class));
finish();
}
});
As you can see, the button is supposed to stop the running Service (created in a previous step), and then finish itself.
When I press the button, the Service.onDestroy is executed just as expected. In the onDestroy I do some cleaning, and then lastly call super.onDestroy():
@Override
public void onDestroy() { // the service onDestroy
// Do some cleaning
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(1);
// more cleaning
Toast.makeText(this, "The service has been stopped! Wii!", Toast.LENGTH_LONG).show();
super.onDestroy();
}
Im my world, that means this Service is dead and buried, along with all the variables in it. Right?
Well, it doesnt seem like it.
The think is, I have a String in my Service that I append text to before I click the button to stop the service. Something like this:
public class SamcomService extends Service {
private String startupText = "";
private void addTextToStartup(String text)
{
startupText += text;
// Sending a broadcast, not relevant
// ...
}
// ...
}
That string, startupText, is not reset when I launch my app again! Its like the Service wasn't killed at all. The startupText contains all the text that was added to it in the previous run.
Why is that? What am I missing? Isnt the Service dead? When I launch the app again, the Service onCreate method is called, implying that it is started from scratch...
--- EDIT ---
I just read this:
What exactly does onDestroy() destroy?
That means that the onDestroy doesnt really destroy anything. Correct? Its pretty lame, and extremely annoying. One well-visited thread here on SO that I create almsot 2 years ago discussing this issue I guess...:
Is quitting an application frowned upon?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以,正如我在上面的编辑中所写的:
我刚刚读了这个: onDestroy() 到底销毁了什么?
这意味着 onDestroy 并没有真正销毁任何东西。正确的?它非常蹩脚,而且非常烦人。我想在两年前创建的一个访问量很大的帖子讨论了这个问题......: 退出应用程序 - 这会让人皱眉吗?
但是,我解决问题的方式当然每个人都会说“不!”到。然而,它工作得很好。我只是将 System.exit(0) 放入服务的 onDestroy 方法中。不再有挥之不去的变量 =)
So, as I wrote in my EDIT above:
I just read this: What exactly does onDestroy() destroy?
That means that the onDestroy doesnt really destroy anything. Correct? Its pretty lame, and extremely annoying. One well-visited thread here on SO that I create almsot 2 years ago discussing this issue I guess...: Quitting an application - is that frowned upon?
However, I solved my problem a way that of course everyone will say "noooo!" to. However, it works nicely. I simply put System.exit(0) in my onDestroy-method in the Service. No more lingering variables =)