我正在开发一种杀死所选应用程序并清除所有数据的工具。模拟this的排序 我只有包名。
答案 0 :(得分:2)
我不确定它是否会起作用,但您可以做的是使用您拥有的软件包名称获取应用程序的进程ID,然后使用进程ID作为参数调用killProcess()
方法。
EDIT1: - 确定..忘记上面写的是什么..我尝试过以下代码,它似乎对我有用。
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
activityManager.killBackgroundProcesses(appProcessName);
您需要在应用程序清单中指定android.permission.KILL_BACKGROUND_PROCESSES
权限,这是非常好的。
EDIT2: - 以下代码清除应用数据。但是有一个问题是它无法清除其他应用程序的数据,因为每个Android应用程序都在Sandbox中运行,这可以防止他们的数据从其范围之外被访问。
public void clearApplicationData(String packageName) {
File cache = getCacheDir();
File appDir1 = new File(cache.getParent()).getParentFile();
File appDir = new File(appDir1.getAbsolutePath() + "/" + packageName);
if (appDir.exists()) {
String[] children = appDir.list();
for (String s : children) {
if (!s.equals("lib")) {
deleteDir(new File(appDir, s));
Toast.makeText(this, "App Data Deleted", Toast.LENGTH_LONG)
.show();
}
}
}
}
public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}
可以通过使用Process
类并通过它执行chmod
命令来更改文件的权限,但是你需要再次运行shell作为该包,这需要应用程序在Android-Manifest
文件中设置为可调试。
因此,如果不是不可能清除其他应用程序的数据,那么底线是很难的。
答案 1 :(得分:0)
在我看来,这似乎非常不可能。 Android不允许您杀死(或清除数据)另一个应用程序。
This link指定方法killBackgroundProcess(String),但是除非它是您自己的包名称,否则内核不会将其终止。