0

My app checks whether an application is running, shows its PID, and aims to kill it on a button click. I am using android.os.Process.killProcess(); but it is just on happening. When I check for it in the Running Applications section in settings, I still find it running. Is there any other way to achieve what I want ?

import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

int pid;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button B = (Button) findViewById(R.id.button);
    TextView PI = (TextView) findViewById(R.id.tV);

    if (isAppRunning("com.whatsapp")) {
        Toast.makeText(this, "Running", Toast.LENGTH_SHORT).show();
        PI.setText(Integer.toString(getPid("com.whatsapp")));
        B.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                android.os.Process.killProcess(getPid("com.whatsapp"));
                Toast.makeText(MainActivity.this, "Killed", Toast.LENGTH_SHORT).show();

            }
        });

    }else
        Toast.makeText(this, "Not Running", Toast.LENGTH_SHORT).show(); 
}   


public boolean isAppRunning (String aApplicationPackageName)
{
            ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
                if (activityManager == null)
                    {
                        return false;
                    }
                    List<RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
                    for(int idx = 0; idx < procInfos.size(); idx++)
                        {   

                            if(procInfos.get(idx).processName.equals(aApplicationPackageName))
                            {   
                                    return true;
                            }
                        }
                                return false;
}

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

2 Answers2

1

This is not allowed; you may only terminate an application that has the same user ID as the one that is attempting the termination. See here for more information: Kill another application on Android?

Community
  • 1
  • 1
Willis
  • 5,308
  • 3
  • 32
  • 61
  • I don't want to kill my app! Instead a 3rd party app. Please look into my code. –  Feb 02 '15 at 21:56
  • 4
    You are not understanding. It is not possible to do this without root. In any case, why do you want to? – Simon Feb 02 '15 at 21:59
0

Seem you are trying to kill a different app. Well, that's not possible.

You can only kill a process that has the same userID as the one that is doing the killing. If you are trying to kill your own process it should work. Otherwise you can't do it (unless you have a rooted device and your application has root priviledges).

Since Android 2.2, you can only kill the background processes of other apps, you won't be able to kill their activities.

If your app is targeting Android <2.2, you can use android.permission.RESTART_PACKAGE.

If you want it to work properly on 2.2 and above, use android.permission.KILL_BACKGROUND_PROCESSES, but, this only kills background services and might "mess up" the other app rather than doing any good.

With the right permissions, you can then do the following:

private ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
am.restartPackage("com.whatsapp");

EDIT Once you get the root privileged (how to do it?) you can use the commands like pm disable com.my.app.package which will kill it. Then use pm enable com.my.app.package so that you can launch it again.

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57