Force Close an app programmatically

那年仲夏 提交于 2019-11-28 22:09:32
SamJ

I found a solution:

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();

Where com.xxxxxx is package name of application to force stop.

you are enclosing the variable packageName with quotes

if(info.processName.equalsIgnoreCase("packageName"))

replace it with if(info.processName.equalsIgnoreCase(packageName))

for force kill, use "kill -9"

so replace String command = "kill "+pid; with

String command = "kill -9 "+pid; 

Also, after os.flush(); line add

os.close(); 
suProcess.waitFor();
Arun Kumar
android.os.Process.killProcess(android.os.Process.myPid());

This works well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!