android 全局異常的處理 詳解


Android游戲《歪把子炮》源碼
http://www.eoeandroid.com/thread-197425-1-1.html

Android 新版捕魚達人源碼
http://www.eoeandroid.com/thread-197437-1-1.html

Android版本的手機RSS閱讀器的源碼
http://www.eoeandroid.com/thread-197465-1-1.html

 

最近新產品測試,頻頻出現異常。所以需要對異常進行全局捕捉。
翻閱大量帖子、源碼終於找到了UncaughtExceptionHandler接口。廢話不多說還是直接上源碼吧。


首先實現UncaughtExceptionHandler

public class CatchHandler implements UncaughtExceptionHandler{
 
        private CatchHandler() {
        }
 
        public static CatchHandler getInstance() {
 
                return mCatchHandler;
        }
 
        private static CatchHandler mCatchHandler = new CatchHandler();
 
        private Context mContext;
 
        private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
 
        @Override
        public void uncaughtException(Thread thread, Throwable ex) {
                if (thread.getName().equals("main")) {
                        ToastException(thread, ex);
                        try {
                                Thread.sleep(3000);
                        } catch (InterruptedException e) {
                        }
                        android.os.Process.killProcess(android.os.Process.myPid());
                        System.exit(1);
                } else {
                        handleException(thread, ex);
                }
 
        }
 
        public void init(Context context) {
                mContext = context;
                Thread.setDefaultUncaughtExceptionHandler(this);
        }
 
        private void ToastException(final Thread thread, final Throwable ex) {
                new Thread() {
                        @Override
                        public void run() {
                                Looper.prepare();
                                StringBuilder builder = new StringBuilder();
                                builder.append("At thread: ").append(thread.getName())
                                                .append("\n");
                                builder.append("Exception is :\n").append(ex.getMessage());
 
                                Toast.makeText(mContext, builder.toString(), Toast.LENGTH_LONG)
                                                .show();
                                Looper.loop();
                        }
                }.start();
        }
 
        private void handleException(final Thread thread, final Throwable ex) {
                Intent intent =new Intent("per.xch.test2_35.main");
                <font color="#00ed00">intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);</font>
                mContext.startActivity(intent);
        }
}

然后在項目中引用

public class CatchApplication extends Application {
        @Override
        public void onCreate() {
                super.onCreate();
                CatchHandler.getInstance().init(getApplicationContext());
        }
}

注意

<application
        <font color="#00ed00">android:name=".CatchApplication"</font>

最后測試下

public class MainActivity extends Activity {
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        new Thread(new Runnable() {
                         
                        @Override
                        public void run() {
                                 throw new NullPointerException("please catch me! sub thread");
                        }
                }).start();
        throw new NullPointerException("please catch me! sub thread");
        
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

之所以在主線程和其他線程采用不同處理方式因為主線程崩潰很大程度上就沒的救了。
還有要注意的是intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
原因很簡單,源碼寫的很明白


ContextImpl.java

  @Override
    public void startActivity(Intent intent) {
        if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
            throw new AndroidRuntimeException(
                    "Calling startActivity() from outside of an Activity "
                    + " context requires the FLAG_ACTIVITY_NEW_TASK flag."
                    + " Is this really what you want?");
        }
        mMainThread.getInstrumentation().execStartActivity(
            getOuterContext(), mMainThread.getApplicationThread(), null,
            (Activity)null, intent, -1);
    }

至於怎么把activity怎么變成dialog我就不廢話嘍,吼,閃人。

 

原文鏈接:http://www.eoeandroid.com/thread-197442-1-1.html

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM