Process.killProcess(Process.myPid());
代码如下
protected void quit() {
int size = activityManager.activityStackCount(); for(int i =size-1 i > 0 i--) {
Activity activity = activityManager.allTaskActivity().get(i); activityManager.popActivity(activity); }
activityManager = null; getActivity().finish();
//目前最为通用的 关闭进程的方法以后的版本使用
Intent startMain = new Intent(Intent.ACTION_MAIN); startMain.addCategory(Intent.CATEGORY_HOME); startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(startMain);
android.os.Process.killProcess(android.os.Process.myPid()); }
android.os.Process.killProcess(appProcessInfo.pid);只能杀死自己所创建的进程,其它进程是杀不掉的,要用到另外一个方法
activityManager.killBackgroundProcesses(processName);,同是权限也要加上 <uses-permission
android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
以下是我的测试代码
public class ListViewActivity extends Activity {
/** Called when the activity is first created. */
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) { killAll(getApplicationContext()); } }); }
public static void killAll(Context context) { //获取一个ActivityManager 对象
ActivityManager activityManager =
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); //获取系统中所有正在运行的进程
List<RunningAppProcessInfo> appProcessInfos =
activityManager.getRunningAppProcesses(); //获取当前activity所在的进程
String currentProcess=context.getApplicationInfo().processName;
//对系统中所有正在运行的进程进行迭代,如果进程名不是当前进程,则Kill掉
for (RunningAppProcessInfo appProcessInfo : appProcessInfos) {
String processName=appProcessInfo.processName;
if (!processName.equals(currentProcess)) {
System.out.println("ApplicationInfo-->"+processName);
activityManager.killBackgroundProcesses(processName);
System.out.println("Killed -->PID:"+appProcessInfo.pid+"--ProcessName:"+processName);
} } } }
代码如下
protected void quit() {
int size = activityManager.activityStackCount(); for(int i =size-1 i > 0 i--) {
Activity activity = activityManager.allTaskActivity().get(i); activityManager.popActivity(activity); }
activityManager = null; getActivity().finish();
//目前最为通用的 关闭进程的方法以后的版本使用
Intent startMain = new Intent(Intent.ACTION_MAIN); startMain.addCategory(Intent.CATEGORY_HOME); startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(startMain);
android.os.Process.killProcess(android.os.Process.myPid()); }
android.os.Process.killProcess(appProcessInfo.pid);只能杀死自己所创建的进程,其它进程是杀不掉的,要用到另外一个方法
activityManager.killBackgroundProcesses(processName);,同是权限也要加上 <uses-permission
android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
以下是我的测试代码
public class ListViewActivity extends Activity {
/** Called when the activity is first created. */
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) { killAll(getApplicationContext()); } }); }
public static void killAll(Context context) { //获取一个ActivityManager 对象
ActivityManager activityManager =
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); //获取系统中所有正在运行的进程
List<RunningAppProcessInfo> appProcessInfos =
activityManager.getRunningAppProcesses(); //获取当前activity所在的进程
String currentProcess=context.getApplicationInfo().processName;
//对系统中所有正在运行的进程进行迭代,如果进程名不是当前进程,则Kill掉
for (RunningAppProcessInfo appProcessInfo : appProcessInfos) {
String processName=appProcessInfo.processName;
if (!processName.equals(currentProcess)) {
System.out.println("ApplicationInfo-->"+processName);
activityManager.killBackgroundProcesses(processName);
System.out.println("Killed -->PID:"+appProcessInfo.pid+"--ProcessName:"+processName);
} } } }