package com.carme.carmerchant; import android.app.Activity; import android.app.ActivityManager; import android.content.Context; import android.support.v7.app.AppCompatActivity; import java.util.Stack; public class AppManager { private static Stack<AppCompatActivity> sActivityStack; private static AppManager sInstance; private AppManager() { } /** * 单一实例 */ public static AppManager getInstance() { if (sInstance == null) { sInstance = new AppManager(); } return sInstance; } /** * 添加Activity到堆栈 */ public void addActivity(AppCompatActivity activity) { if (sActivityStack == null) { sActivityStack = new Stack<AppCompatActivity>(); } sActivityStack.add(activity); } /** * 获取当前Activity(堆栈中最后一个压入的) */ public AppCompatActivity currentActivity() { AppCompatActivity activity = sActivityStack.lastElement(); return activity; } /** * 结束当前Activity(堆栈中最后一个压入的) */ public void finishActivity() { AppCompatActivity activity = sActivityStack.lastElement(); finishActivity(activity); } /** * 结束指定的Activity */ public void finishActivity(AppCompatActivity activity) { if (activity != null) { sActivityStack.remove(activity); activity.finish(); } } /** * 结束指定类名的Activity */ public void finishActivity(Class<?> cls) { for (AppCompatActivity activity : sActivityStack) { if (activity.getClass().equals(cls)) { finishActivity(activity); } } } /** * 从Stack中移除指定Activity */ public void removeActivity(AppCompatActivity activity) { if (activity != null) { sActivityStack.remove(activity); } } /** * 结束所有Activity */ public void finishAllActivity() { try { if (null == sActivityStack) { return; } for (int i = 0, size = sActivityStack.size(); i < size; i++) { if (null != sActivityStack.get(i)) { try { sActivityStack.get(i).finish(); } catch (Exception e) { e.printStackTrace(); } } } sActivityStack.clear(); } catch (Exception e) { e.printStackTrace(); } } /** * 结束除当前外的所有Activity */ public void finishOtherActivity(String className) { for (int i = 0, size = sActivityStack.size(); i < size; i++) { Activity activity = sActivityStack.get(i); if (null != activity && !activity.getClass().getName().equals(className)) { sActivityStack.get(i).finish(); } } sActivityStack.clear(); } /** * 退出应用程序 */ public void AppExit(Context context) { try { finishAllActivity(); ActivityManager activityMgr = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); activityMgr.killBackgroundProcesses(context.getPackageName()); } catch (Exception e) { e.printStackTrace(); } finally { android.os.Process.killProcess(android.os.Process.myPid()); System.exit(1); } } }
退出程序
最新推荐文章于 2023-05-26 10:33:20 发布