在一些低端机型或小内存的ROM中,经常会出现内存不足的情况,除了优化自身程序外,往往需要通过杀死清空一些无关的后台进程来节省内存。但现在许多程序都做了很强的守护进程或加入了平台白名单,常规方法都无法彻底杀死。
比如am.forceStopPackage(),或者 "adb shell killall -9 com.xxx.xxx".都是杀掉后一会又会自启。那么对于这种情况,改如何彻底干掉这些讨厌的家伙呢。
这里提供一种非常规方法,通过禁用该包名来实现,不过需要注意的是,禁用包名后,对应程序进程会被清空,而且无法再启动,应用列表也找不到它,这就需要重新恢复该包名的可用性,这时它是不会自启的。可通过adb命令测试:
禁用程序:adb shell pm disable com.xxx.xxx
恢复程序:adb shell pm enable com.xxx.xxx
至于代码实现,就是通过java 发送shell指令,参考如下:
public static boolean RootCommand(String command)
{
Process process = null;
DataOutputStream os = null;
try {
process = Runtime.getRuntime().exec("sh");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(command + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
if (os != null) {
os.close();
}
process.destroy();
} catch (Exception e) {
}
}
return true;
}
//调用
RootCommand("pm disable com.xxx.xxx");
这样就可以彻底干掉后台顽固进程,不过切记禁用后记得及时恢复该包名的可用性。