Android: Killing (all) Foreground running App

后端 未结 1 403
没有蜡笔的小新
没有蜡笔的小新 2021-01-23 06:49

I am looking for a way to kill the foreground Dalvik App(actively running) from the linux kernel(using it\'s process ID)?

How can I achieve this? any ideas? Does the ke

相关标签:
1条回答
  • 2021-01-23 07:32

    This will get all running processes and kill those with the specified pid:

    ArrayList<Integer> pids = new ArrayList<Integer>();
    ActivityManager  manager = (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> listOfProcesses = manager.getRunningAppProcesses();
    for (ActivityManager.RunningAppProcessInfo process : listOfProcesses)
    {
        if (pids.contains(process.pid))
        {
            // Ends the app
            manager.restartPackage(process.processName);
        }
    }
    

    You will need these permissions to do this:

    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.RESTART_PACKAGES"/>
    
    0 讨论(0)
提交回复
热议问题