如何使用代码退出Android应用程序? [英] How to exit an android app using code?

查看:136
本文介绍了如何使用代码退出Android应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确信这个问题已经被问到次数,因为我读了几个。我的客户希望我把一个按钮放在他的应用程序中,用户可以点击并退出。我已阅读,并发现调用 finish()会这样做。但是,完成只是关闭当前运行的活动吗?我有很多活动,所以在这种情况下,我必须通过每一个活动的实例,完成它们或使每一个活动进入Singleton模式。



我也必须知道 Activity.moveTaskToBack(true)可以让你进入主屏幕。好的,这不是关闭而是背景的过程。那么这是有效的吗?



我应该使用哪种方法来完全关闭应用程序?任何上述描述或任何其他方法/上述方法的其他用法?



请帮助。

解决方案

可以调用System.exit();取消所有的功能。

  submit =(Button)findViewById(R.id.submit); 

submit.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0){
android.os.Process.killProcess (android.os.Process.myPid());
System.exit(1);

}
});


I am sure this question has been asked number of times because I read few. My client wants me to put a button into his app where users can click and exit. I have read this and found calling finish() will do it. But, finish is only closing the current running activity right? I have lot of activities so in this case I have to pass each and every activity's instance and finish them or make every activity into Singleton pattern.

I also got to know Activity.moveTaskToBack(true) can get you into the home screen. OK, this is not closing but backgrounding the process. So is this is effective?

Which method should I use to close the app COMPLETELY? Any of above described or any other method/other usage of above methods?

Please help.

解决方案

You can call System.exit(); to get out of all the acivities.

    submit=(Button)findViewById(R.id.submit);

            submit.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
android.os.Process.killProcess(android.os.Process.myPid());
                    System.exit(1);

                }
            });

这篇关于如何使用代码退出Android应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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