2

我怎样才能在每秒之后杀死一个进程。我想设置一个计时器,它将每秒检查该进程是否已启动,并通过其包名称继续杀死该特定进程

4

2 回答 2

0

在java中你不应该杀死线程。

你能做的是

    Thread thread = new Thread();
    //this you should do when you declare your thread after creating thread object.
    thread.setDaemon(true); 

// 要杀死你的线程使用这个

thread.interrupt();
thread = null;

要杀死其他应用程序进程,请使用

    android.os.Process.killProcess(thread_id);
于 2012-05-25T12:03:03.460 回答
0
void appKiller() {
String nameOfProcess = "location";
ActivityManager  manager = (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> listOfProcesses =  manager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo process : listOfProcesses)
{
    if (process.processName.contains(nameOfProcess))
{
    // Ends the app
    manager.killBackgroundProcesses(process.processName);
    break;
}}

//使用定时器调用appKiller如下。

timer = new Timer("killTimer");
timer.schedule(oTimer, 1000 * 1l,   1000 * 1l );
private TimerTask oTimer = new TimerTask() {
    private void doWork() {
        try {
            appKiller();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
            @Override
    public void run() {

        doWork();
    }
};
于 2012-05-25T12:03:28.900 回答