如何以编程方式强制停止我的android应用程序? [英] How to force stop my android application programmatically?

查看:361
本文介绍了如何以编程方式强制停止我的android应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击closeButton时,我想强制停止我的Android应用程序.这是我的代码.

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();

    }

  });

}

这完成了我的申请.如果转到Settings -> Applications -> Manage applications -> <my application name>,则可以看到强制停止"按钮已启用.这是否意味着我的应用程序没有完全停止?

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?

如何完全完成我的Android应用程序并禁用设置"中的强制停止"按钮?根据我的有限经验,当应用程序中出现异常"(例如NullPointerException)时,它异常停止,看起来好像完全完成了,并且强制停止"按钮看上去已禁用.

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.

推荐答案

另一种方法是

android.os.Process.killProcess(android.os.Process.myPid());

只要您将这些呼叫放在onDestroy()中,我认为这样做并不是那么糟糕. (如果您在事件处理过程中杀死进程,则可能会发生各种不良情况,例如触摸焦点进入以太坊).

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.)

尽管如此,您仍然需要一个令人信服的理由来偏离最佳实践,那就是只调用finish(),让OS负责在必要时终止您的进程.

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应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆