android 全局异常的处理 详解

107 阅读1分钟

Android 新版捕鱼达人源码

www.eoeandroid.com/thread-1974…

Android版本的手机RSS阅读器的源码

www.eoeandroid.com/thread-1974…

最近新产品测试,频频出现异常。所以需要对异常进行全局捕捉。

翻阅大量帖子、源码终于找到了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() {