在 Android 上杀死另一个应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12036895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:32:47  来源:igfitidea点击:

Kill another application on Android?

android

提问by Dinesh Lingam

I am try to kill my another application. But this code is not able to kill my another application. I know to kill another application is a bad idea. But I have a learning purpose, and I have tried to kill. My code part:

我试图杀死我的另一个应用程序。但是这段代码无法杀死我的另一个应用程序。我知道杀死另一个应用程序是个坏主意。但我有一个学习的目的,我试图杀人。我的代码部分:

Button runningApp = (Button) findViewById(R.id.runningApp);
runningApp.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        String nameOfProcess = "com.example.filepath";
        ActivityManager  manager = (ActivityManager)ApplicationActivity.this.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> listOfProcesses = manager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo process : listOfProcesses)
        {
            if (process.processName.contains(nameOfProcess))
            {
                Log.e("Proccess" , process.processName + " : " + process.pid);
                android.os.Process.killProcess(process.pid);
                android.os.Process.sendSignal(process.pid, android.os.Process.SIGNAL_KILL);
                manager.killBackgroundProcesses(process.processName);
                break;
            }
        }
    }
});

I have added Permissions, and they are:

我添加了权限,它们是:

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

Every time I can see the LogCat, the particular application is running in the background. Where am I mistaken?

每次我可以看到LogCat 时,特定的应用程序都在后台运行。我错在哪里?

回答by David Wasser

You can only kill a process that has the same userID as the one that is doing the killing. If you are trying to kill your own process it should work. Otherwise you can't do it (unless you have a rooted device and your application has root priviledges).

您只能终止与执行终止的进程具有相同用户 ID 的进程。如果您试图终止自己的进程,它应该可以工作。否则你不能这样做(除非你有一个 root 的设备并且你的应用程序有 root 权限)。

回答by JohnyTex

If your device is rooted and your app is located in /system/app then you can kill another app by disabling and enabling it via:

如果您的设备已 root 并且您的应用程序位于 /system/app 中,那么您可以通过以下方式禁用和启用另一个应用程序:

pm.setApplicationEnabledSetting(packageName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
pm.setApplicationEnabledSetting(packageName,PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0);

This will kill the app and make it accessible once again. Note that homescreen shortcuts disappear though.

这将杀死该应用程序并使其再次可访问。请注意,主屏幕快捷方式虽然消失了。