Activity管理类,程序退出工具类

Activity管理类, 用于管理所有的Actvity, 提供添加Activity到堆栈,结束特定Activity,结束所有的Activity等方法,在项目开发中都有必备功能:退出功能,而退出功能在项目中一般有退出应用,退出登录两种情况,这里针对这个两种情况都有实现,在此做一下记录,以备以后查阅
  1. package com.centrvideo.parkapp.util;
    import java.util.concurrent.CopyOnWriteArrayList;
    import android.app.Activity;
    import android.app.ActivityManager;
    import android.content.Context;
    import com.centrvideo.parkapp.activity.LoginActivity;
    /**
     * Activity管理类,用于管理Activity,程序退出
     * @ClassName: AppManager 
     * @Description: 
     * @author xiaoxiao
     * @date modify by 2015-8-17 下午1:33:27 
     *
     */
    public class AppManager {
        
        private static CopyOnWriteArrayList<Activity> mActivityStack;
        private static AppManager mAppManager;
        private AppManager() {
        }
        /**
         * 单一实例
         */
        public static AppManager getInstance() {
            if (mAppManager == null) {
                mAppManager = new AppManager();
            }
            return mAppManager;
        }
        /**
         * 添加Activity到堆栈
         */
        public void addActivity(Activity activity) {
            if (mActivityStack == null) {
                mActivityStack = new CopyOnWriteArrayList<Activity>();
            }
            mActivityStack.add(activity);
        }
        /**
         * 结束指定的Activity
         */
        public void killActivity(Activity activity) {
            try {
                if (activity != null) {
                    mActivityStack.remove(activity);
                    activity.finish();
                    activity = null;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        /**
         * 结束指定类名的Activity
         */
        public void killActivity(Class<?> cls) {
            for (Activity activity : mActivityStack) {
                if (activity.getClass().equals(cls)) {
                    killActivity(activity);
                }
            }
        }
        /**
         * 结束所有Activity
         */
        public void killAllActivity() {
            for (int i = 0, size = mActivityStack.size(); i < size; i++) {
                if (null != mActivityStack.get(i)) {
                    mActivityStack.get(i).finish();
                }
            }
            mActivityStack.clear();
        }
        /**
         * 结束所有Activity除了Login
         */
        public void killAllActivityNoLogin() {
            for (int i = 0, size = mActivityStack.size(); i < size; i++) {
                if (null != mActivityStack.get(i)) {
                    if (mActivityStack.get(i).getClass()!=LoginActivity.class) {
                        mActivityStack.get(i).finish();
                    }
                }
            }
            mActivityStack.clear();
        }
        /**
         * 退出应用程序
         */
        @SuppressWarnings("deprecation")
        public void AppExit(Context context) {
            try {
                killAllActivity();
                ActivityManager activityMgr = (ActivityManager) context
                        .getSystemService(Context.ACTIVITY_SERVICE);
                activityMgr.restartPackage(context.getPackageName());
                System.exit(0);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

     


 
 





附件列表

 

posted @ 2015-08-27 17:33  ---风雨无阻--  阅读(1393)  评论(0编辑  收藏  举报