0

我创建了一个服务,该服务创建一个每 5 秒激活一次的定时任务。然后只需检查正在运行的进程,我就有一个我不想运行的应用程序包的列表。因此,当我匹配它们并发现任何不必要的应用程序正在运行时,我会启动一个名为“KillApplication”的活动并将该包传递给它以被杀死。但它不是杀死所需的包。请帮帮我。

public class killApplication extends Activity {

    private DBAdapter db;
    private Context _context;
    String nameOfProcess = "com.android.email";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        _context = this;
        if (getIntent().hasExtra("application")) {
            nameOfProcess = getIntent().getStringExtra("application");
        }
        Log.d("Process Passed", nameOfProcess);
        ActivityManager manager = (ActivityManager) _context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> listOfProcesses = manager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo process : listOfProcesses) {
            Log.e("Process Running", process.processName);
            if (process.processName.contains(nameOfProcess)) { // Ends the app
                android.os.Process.killProcess(process.pid);
                Log.d("Killing Process", nameOfProcess + " : " + process.pid);
                manager.restartPackage(process.processName);
                break;
            }
        }
        finish();
    }

}

我还在清单文件中包含了权限:

4

1 回答 1

0

你为什么要重新启动你杀死的包?(manager.restartPackage(process.processName);)

我会说添加这个:

activityManager.killBackgroundProcesses(String.valueOf(process.processName));
activityManager.killBackgroundProcesses(String.valueOf(process.pid));

在 android.os.process.killprocess 行的顶部。:)

也不要忘记在清单中添加这个:

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

PS:这太频繁了,这会耗尽电池。我的 2 美分

于 2012-06-28T17:48:15.163 回答