如何杀死一个进程? [英] How to kill a process?

查看:60
本文介绍了如何杀死一个进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个活动,第一个活动由启动器启动,第二个活动由第一个启动.当我从第一个活动中终止 进程 时,该进程被终止.但是当我从第二个活动中杀死它时,系统会立即启动一个新进程和第一个活动.(进程的 PID 已更改.)我该如何干净利落地做到这一点?

I have two activities, the first activity is launched by the Launcher, the second is started by the first. When I kill the process from within the first acitivity, the process gets killed. But when I kill it from within the second activity, the system would immediately launch a new process and the first activity. (The process's PID changed.) How can I do it cleanly?

我尝试了 3 种不同的方式来终止进程:System.exit(0)、android.os.Process.killProcess(pid),以及从 Eclipse 的设备面板中以非编程方式.

I tried 3 different ways to kill the process: System.exit(0), android.os.Process.killProcess(pid), and non-programmatically from Eclipse's Devices panel.

以下是我体验过的世界上最简单的两项活动.它们都是各自文件中的外部类.

Following are two world's most simple acitvities that I experiemented with. They both are outer classes in their respective files.

public class FirstActivity extends Activity  {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ((Button)findViewById(R.id.button)).setOnClickListener(
            new OnClickListener() {
                public void onClick(View v) {
                    startActivity(new Intent(FirstActivity.this, 
                        SecondActivity.class));
        }});
    }
}

public class SecondActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_2);
        ((Button)findViewById(R.id.button)).setOnClickListener( 
            new OnClickListener() {
                public void onClick(View v) {
                    // Method 1
                    int pid = android.os.Process.myPid();
                    android.os.Process.killProcess(pid);
                    // Method 2
                    System.exit(0);
        }});
    }
}

(我知道人们说永远不应该提供退出程序的用户界面.但这是出于安全原因.用户必须有办法退出程序并关闭文件,这样他的信息就不会被泄露.)

推荐答案

你可以试试 this.finish()

You can try this.finish()

其他选项会在不通知 android 系统的情况下终止进程,该系统假设用户想要执行的操作已崩溃并重新启动.

The other choices kill the process without informing the android system, which assumes something the user wanted to do has crashed and restarts it.

但是您关于关闭活动以防止数据泄漏的假设是错误的.如果您想提供注销按钮,请执行此操作并让它取消用于获取访问权限的任何访问令牌/密钥.

But your assumptions about closing the activity preventing data leakage is mistaken. If you want to provide a log out button, do that and have it negate whatever access tokens/keys were being used to obtain access.

还要考虑如果其他 Activity 出现在前台而用户没有退出您的活动会发生什么情况 - 例如,如果他们接到电话怎么办?

Also think about what happens if some other activity comes to the foreground without the user exiting yours - for example, what if they receive a phone call?

这篇关于如何杀死一个进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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