0

I am using this code to stop the selected running applications in android phone:

Button view = (Button)findViewById(R.id.button1);
view.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){

String appname = getIntent().getExtras().getString("appname");
String pname=appname.split("\n")[3];
int id=Integer.parseInt(pname);
finish();
android.os.Process.killProcess(id);

The Id is correct but this code is not working... any help?? Is there any other code to kill processes by using their id??

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

3 Answers3

2

if you check the Android API for Kill Process it says the following:

Kill the process with the given PID. Note that, though this API allows us to request to kill any process based on its PID, the kernel will still impose standard restrictions on which PIDs you are actually able to kill. Typically this means only the process running the caller's packages/application and any additional processes created by that app; packages sharing a common UID will also be able to kill each other's processes.

This means you're not able to kill other apps except the ones your app package is in.

This answer explains why it is so and how it works: https://stackoverflow.com/a/7560009/1306012

Community
  • 1
  • 1
Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
1

You need the package name of the app you want to kill (for example com.facebook.katana) and then call

ActivityManager.killBackgroundProcesses(packageName);
//for example: kill the facebook app
//ActivityManager.killBackgroundProcesses("com.facebook.katana);

However, you need the permission KILL_BACKGROUND_PROCESSES defined in your manifest.

Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76
  • Sure you can. See here: http://developer.android.com/reference/android/Manifest.permission.html#KILL_BACKGROUND_PROCESSES – Manuel Allenspach Feb 11 '14 at 17:34
  • killBackground process does not kill the mainActivity of the apps...it only exits some background processes..i tried this its not exiting the application..for example i open adobe reader...then without closing it start the app which stops the existing running processes...i select adobe and stop it using "killbackground processes" and check again...but still adobe reader is not closed..actually i want to stop it.. – user3297438 Feb 12 '14 at 15:49
0

you can use

ActivityManager manager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> services = manager.getRunningAppProcesses();
String service1name = service[1].processName;

you can get all running process's package names, check which one you want to kill, choose that

process get process id by service.pid.

and call

android.os.Process.killProcess(service.pid);
Solenya
  • 694
  • 6
  • 21