Android 程序未知错误、程序奔溃现场处理

来源:互联网 发布:最新淘宝优惠券插件 编辑:程序博客网 时间:2024/06/03 21:22

 

Android程序经常遇到未知名错误,导致程序奔溃,而我们又未在现场,加之网络连接,信号不好等等,一不小心就来个挂掉。

 

我们需要一种安全的办法去处理,保存现场,化未知为可控。

 

java.lang
接口 Thread.UncaughtExceptionHandler

所有已知实现类:
ThreadGroup
正在封闭类:
Thread

public static interface Thread.UncaughtExceptionHandler

Thread 因未捕获的异常而突然终止时,调用处理程序的接口。

当某一线程因未捕获的异常而即将终止时,Java 虚拟机将使用 Thread.getUncaughtExceptionHandler() 查询该线程以获得其UncaughtExceptionHandler 的线程,并调用处理程序的 uncaughtException 方法,将线程和异常作为参数传递。如果某一线程没有明确设置其UncaughtExceptionHandler,则将它的 ThreadGroup 对象作为其 UncaughtExceptionHandler。如果ThreadGroup 对象对处理异常没有什么特殊要求,那么它可以将调用转发给默认的未捕获异常处理程序。

 

 

 

     下面是转载别人的一段代码处理方法:

转自:http://orgcent.com/android-uncaughtexceptionhandler-exception/ | 萝卜白菜的博客

1:异常处理类,

public class CrashHandlerimplements 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 thread,Throwable ex) {
        // if (!handleException(ex) && mDefaultHandler != null) {
        // mDefaultHandler.uncaughtException(thread, ex);
        // } else {
        // android.os.Process.killProcess(android.os.Process.myPid());
        // System.exit(10);
        // }
        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 dialog,int which) {
                                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;
    }
}

 

2:线程绑定,

public class CrashHandlerActivityextends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        CrashHandler crashHandler = CrashHandler.getInstance(); 
        crashHandler.init(this); //传入参数必须为Activity,否则AlertDialog将不显示。
        // 创建错误
        throw new NullPointerException();
    }
}