我创建了一个服务,该服务创建一个每 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();
}
}
我还在清单文件中包含了权限: