【发布时间】:2012-05-25 11:55:35
【问题描述】:
如何在每秒后杀死一个进程。我想设置一个计时器,它会每秒检查进程是否已启动,并通过其包名继续杀死该特定进程
【问题讨论】:
-
看在上帝的份上,你为什么要这么做?除了你将导致你的应用程序每秒持续运行的大量电池消耗之外,我不相信你能够在没有系统签名的情况下杀死不属于你自己的进程。
标签: android process kill-process
如何在每秒后杀死一个进程。我想设置一个计时器,它会每秒检查进程是否已启动,并通过其包名继续杀死该特定进程
【问题讨论】:
标签: android process kill-process
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();
}
};
【讨论】:
在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);
【讨论】: