简体   繁体   English

如何在Android上强制关闭系统关闭应用程序

[英]How to force close app such as system close on Android

I want to force close my app as Android system. 我想强制关闭我的应用程序作为Android系统。
Such as select below step on system. 如选择下面的系统步骤。

"Setting" -> "Apps" -> "my app name" -> "Force stop"  

I want to implement the same behavior by code. 我想通过代码实现相同的行为。
How can I do it? 我该怎么做?

You can write below line of code to kill your App process. 你可以写下面的代码行来杀死你的App进程。 This is what the code runs when you click the Force Stop in 这是单击强制停止时代码运行的内容

Settings --> Manage Applications 设置 - >管理应用程序

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

您可以使用http://developer.android.com/reference/android/os/Process.html来终止您的申请流程。

yes its is possible you have to call public static final void killProcess (int pid) Kill the process with the given PID. 是的,你可能需要调用public static final void killProcess (int pid)使用给定的PID终止进程。 Note that, though this API allows us to request to kill any process based on its PID, the kernel will still impose standard restrictions on which PIDs you are actually able to kill. 请注意,尽管此API允许我们根据其PID请求终止任何进程,但内核仍将对您实际可以杀死的PID施加标准限制。 Typically this means only the process running the caller's packages/application and any additional processes created by that app; 通常,这意味着只有运行调用者的包/应用程序的进程以及该应用程序创建的任何其他进程; packages sharing a common UID will also be able to kill each other's processes. 共享公共UID的软件包也可以杀死彼此的进程。

There are force closes and there are force closes. 有力关闭,有力关闭。 The killProcess does a trick most of the time but there are cases when it is not enough (broadcast messages, for example). killProcess大部分时间都是一个技巧,但有时它还不够(例如广播消息)。

To kill an app completely and for good, simply add the following method to your activity and call it. 要彻底杀死应用程序,只需将以下方法添加到您的活动中并进行调用即可。 What this actually does will be left for the reader as an exercise. 这实际上做的将留给读者作为练习。 :) :)

@SuppressWarnings("null")
public void crash() {
    Object o = null;
    o.hashCode();
}

Super dirty, yes. 超级脏,是的。 But useful if, for example, you really need to simulate a force close (like I do). 但是有用的话,例如,你真的需要模拟一个力接近(就像我一样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM