Android捕获全局异常

Android捕获全局异常

程序避免不了出现bug,导致程序崩溃,为了尽量不影响用户体验,可以全局捕获异常

效果图

  • 异常捕获处理前

Android捕获全局异常_第1张图片

  • 异常捕获处理后(将程序重新启动)

Android捕获全局异常_第2张图片

捕获异常的工具类

package com.example.kongqw.exceptiondemo;

import android.content.Context;
import android.support.v7.internal.app.ToolbarActionBar;
import android.util.Log;
import android.widget.Toast;

/** * Created by kongqw on 2015/11/3. */
public class KqwException implements Thread.UncaughtExceptionHandler {
    private static KqwException myCrashHandler;

    private Context mContext;

    private KqwException(Context context) {
        mContext = context;
    }

    public static synchronized KqwException getInstance(Context context) {
        if (null == myCrashHandler) {
            myCrashHandler = new KqwException(context);
        }
        return myCrashHandler;
    }

    public void uncaughtException(Thread thread, Throwable throwable) {
        long threadId = thread.getId();
        String message = throwable.getMessage();
        String localizedMessage = throwable.getLocalizedMessage();
        Log.i("KqwException", "------------------------------------------------------");
        Log.i("KqwException", "threadId = " + threadId);
        Log.i("KqwException", "message = " + message);
        Log.i("KqwException", "localizedMessage = " + localizedMessage);
        Log.i("KqwException", "------------------------------------------------------");
        throwable.printStackTrace();
        Log.i("KqwException", "------------------------------------------------------");

        // TODO 下面捕获到异常以后要做的事情,可以重启应用,获取手机信息上传到服务器等
        Log.i("KqwException", "------------------应用被重启----------------");
        // 重启应用
        mContext.startActivity(mContext.getPackageManager().getLaunchIntentForPackage(mContext.getPackageName()));
        //干掉当前的程序
        android.os.Process.killProcess(android.os.Process.myPid());
    }
}

使用

在Application下添加Handler

注册Application

在清单文件下添加name属性

<application  …… android:name=".InitApplication" …… >
    <activity  …… >
        <intent-filter>
            ……
        </intent-filter>
    </activity>
</application>

Application

package com.example.kongqw.exceptiondemo;

import android.app.Application;

/** * Created by kongqw on 2015/11/3. */
public class InitApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        KqwException handler = KqwException.getInstance(this);
        Thread.setDefaultUncaughtExceptionHandler(handler);
    }
}

测试类

package com.example.kongqw.exceptiondemo;

import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void bug(View view) {
        Toast.makeText(this, "程序崩溃", Toast.LENGTH_SHORT).show();
        // 除数为0 导致程序崩溃
        int a = 1 / 0;
    }
}

XML布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".MainActivity" tools:showIn="@layout/activity_main">

    <Button  android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="bug" android:text="点击程序崩溃" />
</RelativeLayout>

你可能感兴趣的:(android,bug,异常捕获,全局捕获异常)