1. 程式人生 > >Android開發之如何處理APP意外崩潰問題

Android開發之如何處理APP意外崩潰問題

複製程式碼
public class CrashHandler implements UncaughtExceptionHandler {

/** 獲取CrashHandler例項 */
    public static CrashHandler getInstance() {
        if (INSTANCE == null)
            INSTANCE = new CrashHandler();
        return INSTANCE;
    }
    public void init(CrashHandlerApp app) {
        Log.i("BaseActivity","init()");
        this.app = app;
        // 設定該類為執行緒預設UncatchException的處理器。
        Thread.setDefaultUncaughtExceptionHandler(this);
    }
    /**
     * 當UncaughtException發生時會回撥該函式來處理
     */
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        System.out.println("system wrong....");
        // MBOPApplication app=(MBOPApplication) mainContext;
        // app.setNeed2Exit(true);
        //異常資訊收集
        collectCrashExceptionInfo(thread, ex);
        //應用程式資訊收集
        collectCrashApplicationInfo(app);
        //儲存錯誤報告檔案到檔案。
        saveCrashInfoToFile(ex);
        //MBOPApplication.setCrash(true);
        //判斷是否為UI執行緒異常,thread.getId()==1 為UI執行緒
        if (thread.getId() != 1) {
//            System.out.println("Exception ThreadId" + thread.getId());
            thread.interrupt();
            //TODO 跳轉到IndexActivity
            System.out.println("Thread ID--->" + Thread.currentThread().getId());
//            Intent intent =new Intent(mainContext,IndexActivity.class);
//            actContext.startActivity(intent);
            //彈出對話方塊提示使用者是否上傳異常日誌至伺服器
            new Thread(){
                public void run() {}{                                        
                    Looper.prepare();
                    new AlertDialog.Builder(app.getCurrentAct()).setTitle("異常處理").setMessage("您的程式出現異常,是否將異常資訊上傳至伺服器?")
                    .setPositiveButton("是", new OnClickListener() {
                        
                        public void onClick(DialogInterface dialog, int which) {
                            new Thread(new Runnable() {
                                
                                @Override
                                public void run() {
                                    sendCrashReportsToServer(app,false);
                                    
                                }
                            }).start();
//                            new Thread(){
//                                public void run() {}{    
//                                    try{
//                                        System.out.println("執行上傳執行緒ID"+this.getId());
//                                        this.sleep(5000);
//                                    }catch(Exception e){
//                                        
//                                    }
//                                    sendCrashReportsToServer(app);
//                                }
//                            }.start();                                    
                        }
                    }).setNegativeButton("否", new OnClickListener() {
                        
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            
                        }
                    }).create().show();
                    Looper.loop();                                 
                }
            }.start();
            
        } else {

//            UserSessionCache usc=UserSessionCache.getInstance();
//            ObjectOutputStream oos=null;
//            try {
//                oos.writeObject(usc);
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            SharedPreferences prefenPreferences = mainContext
//            .getSharedPreferences("IsMBOPCrash",Activity.MODE_PRIVATE);
//            SharedPreferences.Editor editor = prefenPreferences.edit();
//            editor.clear();
//            editor.putBoolean("ISCRASH", true);
//            editor.commit();
            
            // 方案一:將所有Activity放入Activity列表中,然後迴圈從列表中刪除,即可退出程式

            for (int i = app.getActivityList().size()-1; i >=0; i--) {
                Activity act = app.getActivityList().get(i);
                act.finish();
            }
            CoreCommonMethod.setCrash(app, true);
            Intent intent = new Intent(app, WelcomeActivity.class);
            intent.putExtra(WelcomeActivity.EXTRA_DIRECT_TO_INDEX, true);
            intent.putExtra(WelcomeActivity.EXTRA_USERINFO, UserSessionCache.getInstance().getUserInfo());
            intent.putExtra(WelcomeActivity.EXTRA_CURRENT_PORTAL_ID, UserSessionCache.getInstance().getCurrentPortalId());
//            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            app.startActivity(intent);                     
            android.os.Process.killProcess(android.os.Process.myPid());  
            
            //方案二:直接使用ActivityManager的restartPackage方法關閉應用程式,
            //此方法在android2.1之後被棄用,不起作用
//            ActivityManager am = (ActivityManager) mainContext.getSystemService(Context.ACTIVITY_SERVICE);
//            am.restartPackage(mainContext.getPackageName());

        }
    }

}