安卓程序崩溃异常处理
 
我们经常会面临这样的情景:应用发布上线后,收到用户反馈说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;
    }
} 相关推荐
  kevinweijc    2020-08-18  
   kikaylee    2020-08-18  
   寻常白昼    2020-08-15  
   shunelly    2020-08-09  
   liangzhouqu    2020-07-28  
   JessePinkmen    2020-07-26  
   xiaoxiaoniaoer    2020-07-21  
   Lexan    2020-06-22  
   heimicms    2020-06-14  
   tianyafengxin    2020-06-08  
   lynjay    2020-06-06  
   cenylon    2020-06-04  
   lqxqust    2020-06-03  
   宿舍    2020-05-29  
   Wonder的学习    2020-05-11  
   明天你好    2020-05-09  
   阿艾辣悟叩德    2020-05-06  
   致终将努力的我们    2020-05-05  
 