Android,使用 : android. 关闭应用程序 os.Process.killProcess(android.os.Process.myPid());

标签 android android-activity exit

在我的项目中,无论如何我都需要关闭应用程序(我知道 Android 不建议这样做,但我的客户需要它)。

大多数情况下,当我们以正常方式(按主页按钮)关闭应用程序时,当您按住主页按钮然后按任务管理器时,您将在后端看到 Activity 应用程序列表。我需要以不显示的方式删除应用。

经过我的研究,我发现这种方法是使用这样的 kill 进程。

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

我在一个测试项目中测试了这个命令,它运行良好。但是,我不知道为什么在这个项目中它充当一个简单的this.finish();。我的意思是当我点击退出菜单时它应该关闭整个应用程序但它只是关闭 Activity 。

我已将这段代码放在一个通用( Activity )类中,所有其他 Activity 都从它继承。

请告诉我问题是什么/哪里?谢谢

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(Menu.NONE, Menu.FIRST, Menu.NONE, "Exit");

    return (super.onCreateOptionsMenu(menu));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case Menu.FIRST:
            android.os.Process.killProcess(android.os.Process.myPid());
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

最佳答案

在你的 AndroidManifest.xml 中:

android:excludeFromRecents="true"

Whether or not the task initiated by this activity should be excluded from the list of recently used applications ("recent apps"). That is, when this activity is the root activity of a new task, this attribute determines whether the task should not appear in the list of recent apps. "true" if the task should be excluded from the list; "false" if it should be included. The default value is "false".

http://developer.android.com/guide/topics/manifest/activity-element.html#exclude

关于Android,使用 : android. 关闭应用程序 os.Process.killProcess(android.os.Process.myPid());,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9410044/

相关文章:

Android:未从 Activity 中调用 onSaveInstanceState

java - 使用两个 TextView 将过滤器添加到 ListView

python - 如何在 Pygame 中退出全屏模式?

c - 如何在我的 C 程序中捕获此 "This application has requested the Runtime to terminate it in an unusual way"错误?

android - gradle-play-publisher发布第一版

android - 应用程式套件语言分割似乎无法处理区域

在 Activity 的 onCreate 中调用 show() 时,Android DialogFragment 不显示

c# - 视觉 C# : Exit the Program Check

java - 显示 DialogFragment 时隐藏系统导航栏

java - 如何更改android抽屉导航中所选项目的背景颜色?