1 package com.itau.jingdong; 2 3 import java.util.Stack; 4 import android.app.Activity; 5 import android.app.ActivityManager; 6 import android.content.Context; 7 8 /** 9 * @author Tau.Chen 陈涛 10 * 11 * @email tauchen1990@gmail.com,1076559197@qq.com 12 * 13 * @date 2013年9月12日 14 * 15 * @version V_1.0.0 16 * 17 * @description 应用程序Activity的管理类 18 * 19 */ 20 public class AppManager { 21 private static Stack<Activity> mActivityStack; 22 private static AppManager mAppManager; 23 24 private AppManager() { 25 } 26 27 /** 28 * 单一实例 29 */ 30 public static AppManager getInstance() { 31 if (mAppManager == null) { 32 mAppManager = new AppManager(); 33 } 34 return mAppManager; 35 } 36 37 /** 38 * 添加Activity到堆栈 39 */ 40 public void addActivity(Activity activity) { 41 if (mActivityStack == null) { 42 mActivityStack = new Stack<Activity>(); 43 } 44 mActivityStack.add(activity); 45 } 46 47 /** 48 * 获取栈顶Activity(堆栈中最后一个压入的) 49 */ 50 public Activity getTopActivity() { 51 Activity activity = mActivityStack.lastElement(); 52 return activity; 53 } 54 55 /** 56 * 结束栈顶Activity(堆栈中最后一个压入的) 57 */ 58 public void killTopActivity() { 59 Activity activity = mActivityStack.lastElement(); 60 killActivity(activity); 61 } 62 63 /** 64 * 结束指定的Activity 65 */ 66 public void killActivity(Activity activity) { 67 if (activity != null) { 68 mActivityStack.remove(activity); 69 activity.finish(); 70 activity = null; 71 } 72 } 73 74 /** 75 * 结束指定类名的Activity 76 */ 77 public void killActivity(Class<?> cls) { 78 for (Activity activity : mActivityStack) { 79 if (activity.getClass().equals(cls)) { 80 killActivity(activity); 81 } 82 } 83 } 84 85 /** 86 * 结束所有Activity 87 */ 88 public void killAllActivity() { 89 for (int i = 0, size = mActivityStack.size(); i < size; i++) { 90 if (null != mActivityStack.get(i)) { 91 mActivityStack.get(i).finish(); 92 } 93 } 94 mActivityStack.clear(); 95 } 96 97 /** 98 * 退出应用程序 99 */ 100 public void AppExit(Context context) { 101 try { 102 killAllActivity(); 103 ActivityManager activityMgr = (ActivityManager) context 104 .getSystemService(Context.ACTIVITY_SERVICE); 105 activityMgr.restartPackage(context.getPackageName()); 106 System.exit(0); 107 } catch (Exception e) { 108 } 109 } 110 }
原文:http://www.cnblogs.com/firecode/p/4937608.html