如何从我的应用程序中停止应用程序

How I can stop an application from my application

我正在编写一个启动器,我想在启动器恢复时终止外部应用程序。我的设备是 root 并且我在清单中设置了权限。

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

我尝试使用此代码终止,但不起作用。

public void killApp(String appPackage) {

    ActivityManager manager =  (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> activityes = ((ActivityManager)manager).getRunningAppProcesses();

    for (int iCnt = 0; iCnt < activityes.size(); iCnt++){
        if (activityes.get(iCnt).processName.contains(appPackage)){
            manager.killBackgroundProcesses(appPackage);
            android.os.Process.sendSignal(activityes.get(iCnt).pid, android.os.Process.SIGNAL_KILL);
            android.os.Process.killProcess(activityes.get(iCnt).pid);
        }
    }
}

怎么了?有没有其他方法可以终止应用程序? 谢谢!

使用 root 你可以这样做:

public void killApp(String appPackage) {

        ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> activityes = ((ActivityManager) manager).getRunningAppProcesses();

        for (int iCnt = 0; iCnt < activityes.size(); iCnt++) {
            if (activityes.get(iCnt).processName.contains(appPackage)) {

                try {
                    Process rootProcess = Runtime.getRuntime().exec(new String[]{"su"});

                    String command = "kill - 9 " + activityes.get(iCnt).pid;

                    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(rootProcess.getOutputStream()), 2048);
                    try {
                        bw.write(command);
                        bw.newLine();
                        bw.flush();
                    } catch (IOException e) {
                        // Handle error
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }
    }