Reputation:
I have this code below:
package com.example.killall;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
//import android.widget.TextView;
import android.app.ActivityManager;
public class MainKill extends Activity {
private Button BprocessesKill ;
//private TextView processesKill;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_kill);
final ActivityManager am=(ActivityManager) getSystemService("SYSTEM_ACTIVITY");
BprocessesKill=(Button) this.findViewById(R.id.BkillProcesses);
//processesKill=(TextView) this.findViewById(R.id.killProcesses);
BprocessesKill.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
am.killBackgroundProcesses(getPackageName());
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_kill, menu);
return true;
}
}
All I want to do is simply to press the button and kill all background processes.. the problem I have with this code is that when I am pressing the button it shows me the message : Unfortunately KillAll(that's my app's name) has stopped. What should I change?
Upvotes: 2
Views: 32890
Reputation: 2387
You can use this code:
List<ApplicationInfo> packages;
PackageManager pm;
pm = getPackageManager();
//get a list of installed apps.
packages = pm.getInstalledApplications(0);
ActivityManager mActivityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
for (ApplicationInfo packageInfo : packages) {
if((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM)==1)continue;
if(packageInfo.packageName.equals("mypackage")) continue;
mActivityManager.killBackgroundProcesses(packageInfo.packageName);
}
Keep in mind that it's very dangerous to kill apps. If you don't exactly know what you're doing, don't use this code please!
Upvotes: 5
Reputation: 543
I use this code to kill my own process (app) :
android.os.Process.killProcess(android.os.Process.myPid());
Upvotes: 6
Reputation: 8641
In short, anyone who shows you how to kill all background processes is doing you a disservice and the Android community a disservice.
Upvotes: 6