Search code examples
androidrootkillpid

How to obtain the PID with the class name and kill the process obtained


I'm trying to get the PID of a process having the class name. The idea is that the user will create a task, select the event, the app and if the app will be opened or killed, so if the user select killed, I want to map the class name in the ComponentName of the intent to the ComponentName in a RunningAppProcessInfo.

The problem is that I'm getting a NullPointerException, and I really can't figure out why, it happens when I check the classname in the if - maybe I'm misunderstanding something... Any ideas?

Note: I'm passing the context in order to have access to the ActivityManager

public static int getPid(Context ctx, Intent intent)
{
    ActivityManager am = (ActivityManager)ctx.getSystemService(Context.ACTIVITY_SERVICE);
    int pid = 0;
    for(RunningAppProcessInfo a : am.getRunningAppProcesses())
    {
        //Revisar TODO


        if(intent.getComponent().getClassName().equals(a.importanceReasonComponent.getClassName()))
        {
            pid = a.pid;
            break;
        }
    }

    return pid;

}

Here is the logcat:

D/su ( 6479): 10106 com.android.test.tasker executing 0 /system/bin/sh using shell /system/bin/sh : sh
D/AndroidRuntime( 6446): Shutting down VM
W/dalvikvm( 6446): threadid=1: thread exiting with uncaught exception (group=0x40015560)
E/AndroidRuntime( 6446): FATAL EXCEPTION: main
E/AndroidRuntime( 6446): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.AIRPLANE_MODE flg=0x20000000 (has extras) } in com.android.test.tasker.events.AirplaneEvent@4051fee8
E/AndroidRuntime( 6446): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722)
E/AndroidRuntime( 6446): at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime( 6446): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 6446): at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime( 6446): at android.app.ActivityThread.main(ActivityThread.java:3835)
E/AndroidRuntime( 6446): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 6446): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 6446): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
E/AndroidRuntime( 6446): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
E/AndroidRuntime( 6446): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 6446): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 6446): at com.android.test.tasker.util.Root.getPid(Root.java:97)
E/AndroidRuntime( 6446): at com.android.test.tasker.events.GenericalEvent.openKill(GenericalEvent.java:35)
E/AndroidRuntime( 6446): at com.android.test.tasker.events.AirplaneEvent.onReceive(AirplaneEvent.java:24)
E/AndroidRuntime( 6446): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709)
E/AndroidRuntime( 6446): ... 9 more

Solution

  • This solution is working on terminal but reboots the phone when is called by the app, is the solution to the problem but i need a fix about the reboot In this case i'm using a script to obtain the pid with pidof, -s is for a single pid and kill -9 . is force close the proc with the pid obtained

    NOTE: The system use the package name to identify the running process

    String command = "pidof -s " + intent.getComponent().getPackageName() + " | kill -9 .";
    
    Process su = Runtime.getRuntime().exec("su");
    DataOutputStream os = new DataOutputStream(su.getOutputStream());
    
    os.writeBytes(command + "\n");
    os.flush();