zoukankan      html  css  js  c++  java
  • 安卓程序崩溃异常处理

     

    我们经常会面临这样的情景:应用发布上线后,收到用户反馈说app崩溃了,但自己重现不了。这时候怎么办呢?

    
    

    很多朋友都会想到用友盟等第三方插件实现,但鉴于安全性要求较高的支付系统,是不允许使用未知来源压缩包/有后门的第三方插件。这时候我们可以考虑自己写一个。

    package System.Interface.free;
    
    import java.lang.Thread.UncaughtExceptionHandler;
    
    import android.app.AlertDialog;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.DialogInterface.OnClickListener;
    import android.os.Looper;
    
    public class CrashHandler implements UncaughtExceptionHandler {
    
        public static final String TAG ="CrashHandler";
        private static CrashHandler INSTANCE =new CrashHandler();
        private Context mContext;
        private Thread.UncaughtExceptionHandler mDefaultHandler ;
        private CrashHandler() {
            
        }
         public static CrashHandler getInstance() {
                return INSTANCE;
            }
         public void init(Context ctx) {
                mContext = ctx;
                mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
                Thread.setDefaultUncaughtExceptionHandler(this);
            }
        @Override
        public void uncaughtException(Thread arg0, Throwable arg1) {
            // TODO Auto-generated method stub
            System.out.println("uncaughtException");
    
            new Thread() {
                @Override
                public void run() {
                    Looper.prepare();
                    new AlertDialog.Builder(mContext).setTitle("提示").setCancelable(false)
                            .setMessage("程序崩溃了...").setNeutralButton("我知道了", new OnClickListener() {
                                @Override
                                public void onClick(DialogInterface arg0, int arg1) {
                                    // TODO Auto-generated method stub
                                    System.exit(0);
                                }
                            })
                            .create().show();
                    Looper.loop();
                }
            }.start();
        }
         /**
         * 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成. 开发者可以根据自己的情况来自定义异常处理逻辑
         * 
         * @param ex
         * @return true:如果处理了该异常信息;否则返回false
         */
        private boolean handleException(Throwable ex) {
            if (ex == null) {
                return true;
            }
            // new Handler(Looper.getMainLooper()).post(new Runnable() {
            // @Override
            // public void run() {
            // new AlertDialog.Builder(mContext).setTitle("提示")
            // .setMessage("程序崩溃了...").setNeutralButton("我知道了", null)
            // .create().show();
            // }
            // });
    
            return true;
        }
    }
  • 相关阅读:
    MySQL:解决脏读问题
    MySQL:隔离性问题(脏读)&回滚演示
    MySQL: Mysql 事务隔离级别
    MySQL:数据库事务
    GRE Vocabulary:sedulous
    MySQL:SQL约束
    GRE Vocabulary:pall
    MySQL:DQL操作单表
    MySQL: DQL 查询表中数据
    MySQL:DML操作 表中数据
  • 原文地址:https://www.cnblogs.com/edangame/p/12585912.html
Copyright © 2011-2022 走看看