天天看點

工具類-ActivityStack

感覺ActivityStack沒啥可以講的主要目的就是将activity存入一個自己定義的棧中再根據自己的需要處理activity中的資訊

代碼如下

public class ActivityStack {

    private final static String TAG = "ActivityHolder";
    private List<FragmentActivity> activityList = new ArrayList<FragmentActivity>();
    ;
    private static ActivityStack instance;

    private ActivityStack() {
    }

    public static synchronized ActivityStack getInstance() {
        if (instance == null) {
            instance = new ActivityStack();
        }
        return instance;
    }

    /**
     * add the activity in to a list end
     *
     * @param activity
     */
    public void addActivity(FragmentActivity activity) {
        try {
            if (activity != null && activityList != null) {
                int size = activityList.size();
                if (checkActivityIsVasivle(activity)) {
                    removeActivity(activity);
                    activityList.add(activityList.size(), activity);
                } else {
                    activityList.add(activity);
                }
                size = activityList.size();
                for (int i = ; i < size; i++) {
                    LogUtils.i("addActivity ==[" + i + "]" + " " + activityList.get(i));
                }

            }
        } catch (Exception e) {
            // TODO: handle exception
        }

    }

    /**
     * 擷取目前Activity(堆棧中最後一個壓入的)
     */
    public Activity currentActivity() {
        Activity activity = activityList.get();
        return activity;
    }

    /**
     * 結束目前Activity(堆棧中最後一個壓入的)
     */
    public void finishCurActivity() {
        Activity activity = currentActivity();
        activityList.remove();
        activity.finish();
    }

    /**
     * finish all the activity in the list.
     * <p/>
     * <p/>
     * the activity calling this method hold the context
     */
    public void finishAllActivity() {
        if (activityList != null) {
            int size = activityList.size();
            for (int i = size - ; i >= ; i--) {
                FragmentActivity activity = activityList.get(i);
                if (activity != null) {
                    activity.finish();
                }
                LogUtils.i("finishAllActivity ==[" + i + "]" + " " + activity);
                activityList.remove(activity);
                // activityList.clear();
            }
        }

    }

    /**
     * finish all the activity in the list.
     * <p/>
     * <p/>
     * the activity calling this method hold the context
     */
    public void finishOtherActivity(FragmentActivity activityCurrent) {
        if (activityList != null) {
            int size = activityList.size();
            for (int i = size - ; i >= ; i--) {
                FragmentActivity activity = activityList.get(i);
                if (activity != null && activity != activityCurrent) {
                    if (activity != null) {
                        activity.finish();
                    }
                    LogUtils.i("finishAllActivity ==[" + i + "]" + " " + activity);
                    activityList.remove(activity);
                }
            }
        }

    }

    /**
     * remove the finished activity in the list.
     *
     * @param activity the activity is removed from activityList
     */
    public void removeActivity(FragmentActivity activity) {
        try {
            if (activityList != null) {
                activityList.remove(activity);
                LogUtils.i("removeActivity==" + " " + activity + "activityList.size===" + activityList.size());
            }
        } catch (Exception e) {
            LogUtils.e("removeActivity" + e.getMessage());
        }
    }


    public void pop(Class<? extends FragmentActivity> activity) {
        for (Activity a : activityList) {
            LogUtils.i(a.getClass().getName() + "--->" + activity.getName());
            if (a.getClass().getName().equals(activity.getName())) {
                if (!a.isFinishing()) {
                    a.finish();
                }
            }
        }
        activityList.remove(activity);
    }


    public boolean checkActivityIsVasivle(FragmentActivity activity) {
        LogUtils.i(" " + activityList.contains(activity));
        return activityList.contains(activity);
    }

    /**
     * <p>
     * 功能 幹掉除home外的所有activity
     * </p>
     *
     * @author sss 時間 2013年12月11日 上午5:38:59
     */
    public void finishWorkActivity() {
        Activity act = null;
        for (int index = ; index < activityList.size(); index++) {
            act = activityList.remove(index);
            if (act == null) {
                continue;
            }
            // if(act.getClass()==HomeActivity.class){
            // continue;
            // }
            act.finish();
        }
    }


    public static boolean isTopActivity(FragmentActivity activity) {
        boolean isTop = false;
        ActivityManager am = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);
        ComponentName cn = am.getRunningTasks().get().topActivity;
        if (cn.getClassName().contains(activity.getClass().getSimpleName())) {
            isTop = true;
        }
        return isTop;
    }
}
           

與之關聯的還有一個處理app異常的類代碼如下

public class AppException extends Exception implements UncaughtExceptionHandler {

    private static final long serialVersionUID = -L;

    private String message;

    private Thread.UncaughtExceptionHandler mDefaultHandler;

    private AppException() {
        super();
        this.mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
    }

    public AppException(String message, Exception excp) {
        super(message, excp);
        this.mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * 擷取APP異常崩潰處理對象
     *
     * @return
     */
    public static AppException getAppExceptionHandler() {
        return new AppException();
    }

    @Override
    public void uncaughtException(Thread thread, Throwable ex) {

        if (!handleException(ex) && mDefaultHandler != null) {
            Log.e("=uncaughtException=","uncaughtException");
            if (InnoFarmApplication.getAppContext()!=null){
               TCAgent.onError(InnoFarmApplication.getAppContext(), ex);
            }
            mDefaultHandler.uncaughtException(thread, ex);
        }
    }

    /**
     * 自定義異常處理
     *
     * @param ex
     * @return true:處理了該異常資訊;否則傳回false
     */
    private boolean handleException(Throwable ex) {
        if (ex == null) {
            return false;
        }
        final String errLog = ex.toString();
        final Activity activity =  ActivityStack.getInstance().currentActivity();

        if (activity == null) {
            return false;
        }

        new Thread() {
            @Override
            public void run() {
                Looper.prepare();
                new AlertDialog.Builder(activity).setTitle("提示")
                        .setCancelable(false).setMessage(errLog)
                        .setNeutralButton("确定", new OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                AppManager.getAppManager().finishAllActivity();
                                android.os.Process.killProcess(android.os.Process.myPid());
                                System.exit();

                            }
                        }).create().show();
                Looper.loop();
            }
        }.start();

        return true;
    }

}
           

這樣的話我們就能判斷異常位置我們的BaseActivity是這樣處理的

public abstract class BaseActivity extends FragmentActivity {
    //  管理所有打開的activity
    //  static  和對象沒有關系  和類有關系
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       Thread.setDefaultUncaughtExceptionHandler(AppException.getAppExceptionHandler());
        init();
        initView();
        ActivityStack.getInstance().addActivity(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityStack.getInstance().removeActivity(this);
    }
    /**
     * 初始化
     */
    protected abstract void init();
    /**
     * 初始化view界面
     */
    protected abstract  void initView() ;
}