Android全局捕获异常机制

时间:2024-05-18 15:46:59

个人见解:             

全局捕获异常就是在程序出现异常报错时,能够快速捕捉的错误信息,通过实现Thread.UncaughtExceptionHandler接口操作捕获异常信息,能够快速定位问题提高开发效率,并可以给用户视觉反应,不让程序直接崩溃。其实为我们的项目提供一个异常捕获跟踪处理机制,我认为应包含捕获异常、写入异常数据到SD卡中、方便过后解决问题、最终目标为解决异常从而提高代码的健壮性。

视图:

Android全局捕获异常机制

Demo:

/**
 * @Auther: Mac
 * @Date: 2019/2/22 20:19:47
 * @Description: 异常捕获工具类
 */
public class CrashHandler implements Thread.UncaughtExceptionHandler {

    private Context mContext;

    private Thread.UncaughtExceptionHandler exceptionHandler;
    /**
     * 单例模式
     */

    private static CrashHandler crashHandler = null;

    private CrashHandler() {
    }

    public static CrashHandler getInstance() {
        if (crashHandler == null) {
            synchronized (CrashHandler.class) {
                crashHandler = new CrashHandler();
            }
        }
        return crashHandler;
    }

    /**
     * 初始化
     * @param context
     */
    public void init(Context context) {
        mContext = context;
        exceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(this);
    }

    @Override
    public void uncaughtException(Thread t, Throwable e) {
        /**
         * 判断是否有异常
         * 有异常调用
         */
        if (hanlderException(e) && crashHandler != null) {
            crashHandler.uncaughtException(t, e);
        } else {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e1) {
                Log.i("xxx", "error: " + e1);
            }
        }
    }

    /**
     * 判断异常
     *
     * @param e
     * @return
     */
    private boolean hanlderException(final Throwable e) {
        if (e == null) {
            return false;
        }

        /**
         *   手动处理
         */
        new Thread(new Runnable() {
            @Override
            public void run() {
                Looper.prepare();
                Log.i("xxx", "error: " + e.toString());
                Toast.makeText(mContext, "程序开小差了...", Toast.LENGTH_SHORT).show();
                /**
                 * 写入SD卡
                 */
                String sdFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/crash.txt";
                File file = new File(sdFile);
                try {
                    if (!file.exists()) {
                        file.createNewFile();
                    }
                    FileOutputStream outputStream = new FileOutputStream(file);
                    outputStream.write(e.toString().getBytes());
                    outputStream.close();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
                Looper.loop();
            }
        }).start();
        return true;
    }
}

初始化: 

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        //初始化CrashHandler
        CrashHandler.getInstance().init(this);
    }
}

清单文件:(记得清单文件初始化)

    android:name=".app.MyApplication"
   

 

log.i("mua","记得点赞!!!");