android 崩溃后友好弹框提醒

  • 公司产品,APP崩溃是在所难免的,崩溃后最后进行友好提醒

首先配置系统Applictaion,代码如下,其中CrashHandler.getInstance(this);为崩溃处理,直接贴代码

public class SluiceApplictaion extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        // 初始化阿里云日志部分
        new Thread(() -> IPService.getInstance().asyncGetIp(IPService.DEFAULT_URL, handlerAli)).start();
        CrashHandler.getInstance(this);
    }

    /**
     * 接受阿里云日志服务返回信息
     */
    private Handler handlerAli = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case IPService.HANDLER_MESSAGE_GETIP_CODE:
                    AliLogUtils.SOURCE_IP = (String) msg.obj;
                    return;
            }
            super.handleMessage(msg);
        }
    };
}

其次是CrashHandler类,主要用于捕捉APP崩溃,崩溃后,这里的思路是跳转到一个activity,到了activity内之后,想弹窗或者别的,就随意了。也直接贴代码

package cn.storeteam.sluice.base;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import java.io.PrintWriter;
import java.io.StringWriter;
import cn.storeteam.sluice.BuildConfig;
import top.xport.framework.base.CrashDialogActivity;

/**
 * 功能描述:系统崩溃集中处理类
 * Created by Yanfulei on 2017/11/30.
 */

public class CrashHandler implements Thread.UncaughtExceptionHandler {
    private static CrashHandler instance;
    private Application application;
    private Thread.UncaughtExceptionHandler mDefaultHandler;

    /**
     * 保证只有一个CrashHandler实例
     */
    private CrashHandler(Context context) {
        application = (Application) context.getApplicationContext();
        mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(this);
    }

    /**
     * 获取CrashHandler实例 ,单例模式
     */
    public static CrashHandler getInstance(Context context) {
        CrashHandler inst = instance;
        if (inst == null) {
            synchronized (CrashHandler.class) {
                inst = instance;
                if (inst == null) {
                    inst = new CrashHandler(context.getApplicationContext());
                    instance = inst;
                }
            }
        }
        return inst;
    }

    /**
     * 当UncaughtException发生时会转入该函数来处理
     */
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        try {
            StringWriter stringWriter = new StringWriter();
            PrintWriter printWriter = new PrintWriter(stringWriter);
            ex.printStackTrace(printWriter);
            printWriter.close();
            String unCaughtException = stringWriter.toString();//详细错误日志
            Log.e("崩溃信息", unCaughtException);
            // 提示信息
            Intent intent = new Intent(application, CrashDialogActivity.class);
            CrashDialogActivity.ErrorBean errorBean = new CrashDialogActivity.ErrorBean();
            errorBean.setALI_SLS_LOG_TYPE(BuildConfig.ALI_SLS_LOG_TYPE);
            errorBean.setErrorMsg(unCaughtException);
            errorBean.setSN(SystemStatic.PROPERTY_IMSI);
            errorBean.setStoreName(SystemStatic.BASE_INFO.getDeviceInfo().getStore().getStoreName());
            intent.putExtra("error_bean", errorBean);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            application.startActivity(intent);
            System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mDefaultHandler.uncaughtException(thread, ex);
    }
}

接着是CrashDialogActivity类,主要做的是一个透明的activity,外加一个dialog提示用户系统崩溃了。因为我们是开发板,所以都可以获取root,崩溃后,整机重启,或者进行别的处理,也直接贴代码

package top.xport.framework.base;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.widget.TextView;
import java.io.Serializable;
import top.xport.framework.R;
import top.xport.framework.utils.AliLogUtils;
import top.xport.framework.utils.ShellUtils;
import top.xport.framework.widget.CustomToastDialog;

/**
 * 崩溃弹框
 */
public class CrashDialogActivity extends Activity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_carash_dialog);
        CustomToastDialog customBaseDialog = new CustomToastDialog(this);
        customBaseDialog.setDialogMsg("5 秒后整机重启。错误日志正在上传,请勿操作!");
        customBaseDialog.setDialogTypeSuccess(false);
        customBaseDialog.show();
        customBaseDialog.setDialogClose(false);
        TextView tvErrorTitle = customBaseDialog.getDialogView().findViewById(R.id.tv_error_title);
        tvErrorTitle.setText("致命错误!");
        ErrorBean errorBean = (ErrorBean) getIntent().getSerializableExtra("error_bean");
        // 上传至阿里云
        AliLogUtils.error(errorBean.getStoreName(), errorBean.getSN(), "错误", errorBean.getErrorMsg(), errorBean.getALI_SLS_LOG_TYPE());
        new CountDownTimer(6 * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                customBaseDialog.setDialogMsg("" + millisUntilFinished / 1000 + " 秒后整机重启。错误日志正在上传,请勿操作!");
            }

            @Override
            public void onFinish() {
                // 执行重启命令
                ShellUtils shellUtils = new ShellUtils();
                shellUtils.run("reboot", 2000);
            }
        }.start();
    }

    public static class ErrorBean implements Serializable {

        private String storeName;
        private String SN;
        private String errorMsg;
        private String ALI_SLS_LOG_TYPE;

        public String getStoreName() {
            return storeName;
        }

        public void setStoreName(String storeName) {
            this.storeName = storeName;
        }

        public String getSN() {
            return SN;
        }

        public void setSN(String SN) {
            this.SN = SN;
        }

        public String getErrorMsg() {
            return errorMsg;
        }

        public void setErrorMsg(String errorMsg) {
            this.errorMsg = errorMsg;
        }

        public String getALI_SLS_LOG_TYPE() {
            return ALI_SLS_LOG_TYPE;
        }

        public void setALI_SLS_LOG_TYPE(String ALI_SLS_LOG_TYPE) {
            this.ALI_SLS_LOG_TYPE = ALI_SLS_LOG_TYPE;
        }
    }
}

主要是写给自己做记录的

你可能感兴趣的:(android 崩溃后友好弹框提醒)