Force Close an app programmatically
Asked Answered
M

4

5

I need to make an application that can programmatically do what the force stop button does to applications. (I am using root permissions)

ScreenShot

I have tried this code:

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;
}

But the problem is that the process isn't found, even though the force stop button isn't grayed out.

Even Using:

                         Process suProcess = Runtime.getRuntime().exec("su");
                        DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());


                        os.writeBytes("kill "+ APP_PID + "\n");

                        os.flush();

Doesn't do what the force stop button does since after i execute this code the force stop button isn't grayed out.

Using root how can I programmatically do what the force stop button in the App info does?

Massotherapy answered 3/10, 2014 at 20:9 Comment(1)
do you have any solution for non root?Tyrolienne
M
13

I found a solution (which requires a rooted device):

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.

Massotherapy answered 3/10, 2014 at 20:39 Comment(1)
No it doesn't. All it does is minimizes the app. It doesn't completely force shutdown an app. Plus, it only minimizes the foreground app.Gummy
L
2

you are enclosing the variable packageName with quotes

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

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

Logia answered 3/10, 2014 at 20:20 Comment(1)
You were right. I fixed the bug but I'm still unable to force stop the app using root.Massotherapy
B
2
android.os.Process.killProcess(android.os.Process.myPid());

This works well.

Blackford answered 15/5, 2015 at 12:56 Comment(0)
L
1

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();
Logia answered 3/10, 2014 at 20:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.