在我的项目中,无论如何我都需要关闭应用程序(我知道 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/