• 微软原版系统

  • 一键重装系统

  • 纯净系统

  • 在线技术客服

魔法猪系统重装大师 一键在线制作启动 U 盘 PE 系统 用一键重装的魔法拯救失去灵魂的系统
当前位置:首页 > 教程 > 电脑教程

android异常处理对异常进行全局捕捉

时间:2015年04月02日 15:29:35    来源:魔法猪系统重装大师官网    人气:9521

最近新产品测试,频频出现异常。所以需要对异常进行全局捕捉。
翻阅大量帖子、源码终于找到了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");
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(intent);
        }
}

 

然后在项目中引用

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

注意

android:name=".CatchApplication"

最后测试下

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我就不废话喽,吼,闪人。

android,异常,处理,对,进行,全局,捕捉,最近,
栏目:电脑教程 阅读:1000 2023/12/27
Win7教程 更多>>
U盘教程 更多>>
Win10教程 更多>>
魔法猪学院 更多>>

Copyright © 2015-2023 魔法猪 魔法猪系统重装大师

本站发布的系统仅为个人学习测试使用,请在下载后24小时内删除,不得用于任何商业用途,否则后果自负,请支持购买微软正版软件。

在线客服 查看微信 返回顶部