如何以编程方式强制停止我的 Android 应用程序?
我想在单击 closeButton 时强制停止我的 Android 应用程序。这是我的代码。
protected void onCreate(Bundle savedInstanceState) {
this.setContentView(R.layout.layoutxml);
this.closeButton = (Button)this.findViewById(R.id.close);
this.closeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
我的申请到此结束。如果我进入设置 ->应用->管理应用程序 -> <我的应用程序名称>
,我可以看到“强制停止”按钮已启用。这是否意味着我的应用程序没有完全停止?
如何才能完全完成我的 Android 应用程序并禁用“设置”中的“强制停止”按钮?根据我有限的经验,当应用程序中发生“异常”(例如 NullPointerException)时,它会异常停止,看起来完全完成,并且“强制停止”按钮看起来被禁用。
I'd like to force stop my Android application when I click closeButton. This is my code.
protected void onCreate(Bundle savedInstanceState) {
this.setContentView(R.layout.layoutxml);
this.closeButton = (Button)this.findViewById(R.id.close);
this.closeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
This finishes my application. If I go to Settings -> Applications -> Manage applications -> <my application name>
, I can see the 'Force Stop' button is enabled. Does this mean my application was not stopped completely?
How can I finish my Android application completely and disable the 'Force Stop' button inthe 'Settings'? From my limited experience, when an 'Exception' (ex. NullPointerException) occurs in the application, it stops abnormally, looks like it finished completely, and the 'Force Stop' button looks disabled.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
另一种方法是,
我认为这样做并没有那么糟糕,只要您将这些调用放在
onDestroy()
中即可。 (如果您在事件处理过程中终止进程,则可能会发生各种不好的事情,例如触摸焦点进入以太。)然而,您需要一个令人信服的理由来偏离最佳实践,即只调用
finish()
并让操作系统在需要时负责终止您的进程。Another way is
I don't think it's all that bad to do this, provided you put those calls in
onDestroy()
. (If you kill your process in the middle of event handling, all kinds of bad things—like the touch focus going into the ether—can happen.)Nevertheless, you need a compelling reason to deviate from best practice, which is to just call
finish()
and let the OS take care of killing off your process when/if it needs to.注意:这不会杀死整个应用程序,但如果您想要完成所有应用程序活动,这是最好的选择。
Android ≥ 16
finishAffinity();
Android
finishAffinity();
16ActivityCompat.finishAffinity(Activity Activity)
希望这有帮助
Note: This does not kill the entire app, but if what you want to do is to finish all the app activities, this is the best option.
Android ≥ 16
finishAffinity();
Android < 16
ActivityCompat.finishAffinity(Activity activity)
Hope this helps
终止应用程序的一个不好的方法是
System.exit(0)
编辑:
我相信我欠一些解释。 Android 自行处理应用程序生命周期,您不应该“强制关闭”它,而且我不知道有什么好的方法可以做到这一点。一般来说,如果您的应用程序仍然在后台运行,那么如果用户再次启动它,它会很快弹出。
A bad way to kill the application would be
System.exit(0)
Edit:
I believe I owe some explanation. Android handles the application lifecycle on its own, and you are not supposed to 'ForceClose' it, and I don't know any good way to do it. Generally its ok if your application is still alive in the background, this way if user launches it again it will pop up quickly.
我知道这是一个迟到的回复,希望这对某人有所帮助。
您可以在代码片段中尝试使用
finishAndRemoveTasks();
而不是finish();
。这将杀死您的应用程序的所有活动和所有进程,甚至从任务管理器中删除最近的应用程序。
注意:如果您在代码中使用了任何类型的
handler
或thread
,请确保删除其功能,然后使用上面建议的代码,否则会出现NullPointer Exception
或ResourceNotFound Exception
将会发生。I know it is a late reply , hope this helps some one.
You can try
finishAndRemoveTasks();
instead offinish();
in your snippet.This would kill your application's all activities and all process and even remove for recent apps from task manager.
Note: If you have use any kind of
handler
orthread
in your code make sure you remove its functionalities and then use the above suggested code , if notNullPointer Exception
orResourceNotFound Exception
would occur.简短而简单
Short and simple
为什么不对 ActivityManager 进行 Shell 调用?
Why not to make a Shell-Call to ActivityManager?
下面的链接有解决方案。
它对我有用。
如何以编程方式强制停止我的 Android 应用程序?
The link below has the solution.
Its worked for me.
How to force stop my android application programmatically?