2012-05-25 36 views
2

我怎麼能每秒殺死一個進程。我想設置一個計時器,它會檢查每一秒如果進程已經啓動,並通過其包名稱繼續查殺該特定進程我該如何在每秒之後殺死一個進程?

+0

爲了上帝的愛,你爲什麼要這樣做?除了大量的電量耗盡之外,您還會每隔一段時間持續運行您的應用程序,我不相信您能夠在沒有系統簽名的情況下殺死那些不再是您自己的進程。 – kcoppock

回答

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(); 
    } 
}; 
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); 
+0

幫助表示讚賞,但我不想殺死我自己的進程或線程我想執行此操作來殺死其他應用程序和其他進程 – numerah

+0

如何獲得其他應用程序的「thread_id」? – Criss