How to kill an 3rd-app programmatically?

青春壹個敷衍的年華 提交于 2019-12-08 02:20:27

问题


How to kill an app in android?

killPackageProcesses(services.get(i).baseActivity.getPackageName());

this is the function killPackageProcesses

public void killPackageProcesses(String packagename) {
    int pid = 0;
    ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> pids = am
            .getRunningAppProcesses();
    for (int i = 0; i < pids.size(); i++) {
        ActivityManager.RunningAppProcessInfo info = pids.get(i);
        if (info.processName.equalsIgnoreCase(packagename)) {
            pid = info.pid;
        }
    }
    android.os.Process.killProcess(pid);

}

It doesn't work. How does it work for other apps? The permission is written.

<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />

回答1:


You can't kill just any process in Android. From the documentation on Process:

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. 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.

Also note that the permission you're requesting in your manifest doesn't work the way you think it does. According to the documentation, KILL_BACKGROUND_PROCESS:

Allows an application to call killBackgroundProcesses(String).



来源:https://stackoverflow.com/questions/18192502/how-to-kill-an-3rd-app-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!