我需要创建一个能够以编程方式执行强制停止按钮对应用程序执行操作的应用程序。 (我使用 root 权限)
我试过这段代码:
private void killApp(){
try {
int pid = getPid(com.XXX);
if(pid != -1){
String command = "kill "+pid;
Log.i("Got PID",pid + "");
try {
Process suProcess = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
os.writeBytes(command + "\n");
os.flush();
Log.i("Executed","Kill " + pid);
} catch (IOException e) {
// Handle error
}
} else {
Log.i("Not Found","App Not Found");
}
} catch (Exception e) {
e.printStackTrace(); // Device not rooted!
}
}
private int getPid(String packageName){
ActivityManager am = (ActivityManager)getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> pids = am.getRunningAppProcesses();
int processid = 0;
for(int i = 0; i < pids.size(); i++) {
ActivityManager.RunningAppProcessInfo info = pids.get(i);
Log.i("PID",pids.get(i) + "");
Log.i("PID Package",info.processName);
if(info.processName.equalsIgnoreCase(packageName)){
processid = info.pid;
return processid;
}
}
return -1;
}
但问题是找不到该过程,即使强制停止按钮不是变灰。
即使使用:
Process suProcess = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
os.writeBytes("kill "+ APP_PID + "\n");
os.flush();
执行此代码后强制停止按钮
不执行强制停止按钮的操作。使用root如何以编程方式执行App info中的强制停止按钮?
答案 0 :(得分:12)
我找到了解决方案:
Process suProcess = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
os.writeBytes("adb shell" + "\n");
os.flush();
os.writeBytes("am force-stop com.xxxxxx" + "\n");
os.flush();
其中com.xxxxxx
是强制停止的应用程序包名称。
答案 1 :(得分:2)
您将变量packageName
括在引号
if(info.processName.equalsIgnoreCase("packageName"))
将其替换为if(info.processName.equalsIgnoreCase(packageName))
答案 2 :(得分:1)
强制kill
,请使用"kill -9"
所以用{/ p>替换String command = "kill "+pid;
String command = "kill -9 "+pid;
此外,在os.flush();
行添加
os.close();
suProcess.waitFor();
答案 3 :(得分:1)
for (int h = 0; h < jsonArrr.length(); h++)
{
Log.e(TAG,"JSONObjet at "+h+" : "+jsonArrr.get(h).toString());
}
这很有效。