我创建了一项服务,创建了一个每5秒激活一次的timmer任务。然后只检查正在运行的进程,我有一个我不想运行的应用程序包列表。因此,当我匹配它们并发现任何不必要的应用程序运行时,我启动一个名为“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();
}
}
我还在Manifest文件中包含了权限:
答案 0 :(得分: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美分