如何以编程方式退出 Android 应用程序? [英] How to exit an Android app programmatically?

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

问题描述

我确信这个问题已经被问过很多次了,因为我读了一些.我的客户希望我在他的应用程序中放置一个按钮,用户可以在其中单击并退出.我已阅读 this 并发现调用 finish() 会做到的.但是,完成只是关闭当前正在运行的活动,对吗?我有很多活动,所以在这种情况下,我必须传递每个活动的实例并完成它们或将每个活动都设置为单例模式.

我还了解到 Activity.moveTaskToBack(true) 可以让您进入主屏幕.好的,这不是关闭而是后台进程.那么这样有效吗?

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

解决方案

API 16 中发布的finishAffinity 方法会关闭所有正在进行的活动并关闭应用程序:

this.finishAffinity();

<块引用>

完成此活动以及当前任务中紧邻其下方的所有具有相同关联的活动.这通常用于当应用程序可以启动到另一个任务(例如从它理解的内容类型的 ACTION_VIEW)并且用户已经使用向上导航切换出当前任务并切换到它自己的任务时.在这种情况下,如果用户向下导航到第二个应用程序的任何其他活动,作为任务切换的一部分,所有这些都应该从原始任务中删除.

请注意,此完成不允许您将结果传递给前一个活动,如果您尝试这样做,则会引发异常.


从 API 21 开始,您可以使用:

finishAndRemoveTask();

<块引用>

完成此任务中的所有活动并将其从最近的任务列表中删除.

替代方案:

getActivity().finish();System.exit(0);


int pid = android.os.Process.myPid();android.os.Process.killProcess(pid);


Process.sendSignal(Process.myPid(), Process.SIGNAL_KILL);


Intent i = new Intent(context, LoginActivity.class);i.putExtra(EXTRA_EXIT, true);i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);context.startActivity(i);

来源:如何以编程方式退出安卓应用程序


希望能帮助到你!祝你好运!

I am sure this question has been asked number of times because I read a 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?

解决方案

The finishAffinity method, released in API 16, closes all ongoing activities and closes the app:

this.finishAffinity();

Finish this activity as well as all activities immediately below it in the current task that have the same affinity. This is typically used when an application can be launched on to another task (such as from an ACTION_VIEW of a content type it understands) and the user has used the up navigation to switch out of the current task and in to its own task. In this case, if the user has navigated down into any other activities of the second application, all of those should be removed from the original task as part of the task switch.

Note that this finish does not allow you to deliver results to the previous activity, and an exception will be thrown if you are trying to do so.


Since API 21, you can use:

finishAndRemoveTask();

Finishes all activities in this task and removes it from the recent tasks list.

Alternatives:

getActivity().finish();
System.exit(0);


int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);


Process.sendSignal(Process.myPid(), Process.SIGNAL_KILL);


Intent i = new Intent(context, LoginActivity.class);
i.putExtra(EXTRA_EXIT, true);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(i);

Source: How to quit android application programmatically


Hope it helps! Good Luck!

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

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