如何杀死所有活动的应用程序?应用程序

2023-09-11 12:19:59 作者:捧脸亲亲

可能重复:   Quitting一个应用程序 - 这是令人难以接受的

我要为用户提供一个选项,退出应用程序,因为我需要删除一些敏感的数据,只要在应用软件需要存储在共享preferences。

I want to offer the user an option to exit the application as I need to delete some sensitive data, which is stored in the SharedPreferences as long as the application needs it.

当用户想退出,在共享preferences密码应被消灭,当然还有应用程序的所有活动应关闭(这是没有意义的,而不已知的密码运行它们 - 他们会崩溃)。

As soon as the user wants to exit, the password in the SharedPreferences should be wiped and of course all activities of the application should be closed (it makes no sense to run them without the known password - they would crash).

我怎么能这样做?

System.exit(0)完成()只退出当前的活动 - 无用的。我知道有一个任务管理器应用程序。一个是如何做的呢?它能够杀死整个应用程序...

System.exit(0) and finish() only exit the current activity - useless. I know there is a taskmanager app. How is that one doing it? It's able to kill the whole application...

推荐答案

在使用完成方法,它不关闭整个过程中,它工作在后台。

When you use the finish method, it is not close the complete process, it is working in background.

请使用code的主要活动(请不要在每个活动或子活动中使用):

Please use this code in Main Activity (Please don't use in every activities or sub Activities):

@Override
public void onBackPressed() {

    android.os.Process.killProcess(android.os.Process.myPid());
    // This above line close correctly
}