android应用捕获运行异常发送反馈邮件的实现

 此功能的意义:

 android应用很多时候有异常崩溃,导致用户体验很不好,而且开发者无法捕获到相关错误信息,也无法知悉。所以将主线程的崩溃信息,已邮件的形式或者 由GA统计事件上传给开发者,这是很有必要的。

实现思路:

     java中线程Thread对象有方法setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler exceptionHandler),当线程发生运行时异常而中断时,ExceptionHandler对象就能捕捉到该信息。需要单独处理此异常,需要重写该类的public void uncaughtException(Thread arg0, Throwable arg1) 方法。

class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler{


public void uncaughtException(Thread arg0, Throwable arg1) {
System.out.println("======"+arg1);
}
 }

在android应用中应用:

众所周知,android主线程也是一个线程,那我们也可以给它添加ExceptionHandler,具体操作我们在application oncreate的时候初始化我们自己定义好的ExceptionHandler对象,然后设置给主线程。下面是具体实现类的例子。

public class CrashHandler implements UncaughtExceptionHandler {
/** Debug Log tag */
public static final String TAG = "CrashHandler";
/**
* 是否开启日志输出,在Debug状态下开启, 在Release状态下关闭以提示程序性能
* */
/** 系统默认的UncaughtException处理类 */
private Thread.UncaughtExceptionHandler mDefaultHandler;
/** CrashHandler实例 */
private static CrashHandler INSTANCE;
/** 程序的Context对象 */
private Context mContext;


/** 保证只有一个CrashHandler实例 */
private CrashHandler() {
}


/** 获取CrashHandler实例 ,单例模式 */
public static CrashHandler getInstance() {
if (INSTANCE == null) {
INSTANCE = new CrashHandler();
}
return INSTANCE;
}


/**
* 初始化,注册Context对象, 获取系统默认的UncaughtException处理器, 设置该CrashHandler为程序的默认处理器

* @param ctx
*/
public void init(Context ctx) {
mContext = ctx;
mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(this);
}


/**
* 当UncaughtException发生时会转入该函数来处理
*/
@Override
public void uncaughtException(Thread thread, Throwable ex) {
AVLog.d(TAG, "catch uncaughtException:" + ex);
ex.printStackTrace();
if (!handleException(ex) && mDefaultHandler != null) {
AVLog.d(TAG, "user didn't handle exception");
// 如果用户没有处理则让系统默认的异常处理器来处理
mDefaultHandler.uncaughtException(thread, ex);
} else {
AVLog.d(TAG, "uncaughtException kill process");
// Sleep一会后结束程序
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
AVLog.e(TAG, "Error : "+e);
}
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
}
private boolean handleException(Throwable ex) {
if (ex == null) {
return true;
}

String result = getStringFromThrowable(ex);
// 在此你可以将result数据上传ga或者由邮件发送或者写sdcard然后上传服务器。
return false;
}

//  从Throwable中获取异常信息
   private  String getStringFromThrowable(Throwable ex) {
       Writer info = new StringWriter();
       PrintWriter printWriter = new PrintWriter(info);
       ex.printStackTrace(printWriter);


       Throwable cause = ex.getCause();
       while (cause != null) {
           cause.printStackTrace(printWriter);
           cause = cause.getCause();
       }


       String result = info.toString();
       printWriter.close();


       return result;
   }

}
在application oncreate方法中调用:
  CrashHandler crashHandler = CrashHandler.getInstance();
// 注册crashHandler
 crashHandler.init(getApplicationContext());

ok,此功能介绍完成。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
捕获 Android 应用程序的异常并重启应用程序,可以使用 Thread.UncaughtExceptionHandler 接口。该接口用于捕获捕获异常,并在捕获异常后重启应用程序。 下面是一个简单的示例代码,用于设置应用程序的 UncaughtExceptionHandler: ``` public class MyApplication extends Application implements Thread.UncaughtExceptionHandler { @Override public void onCreate() { super.onCreate(); Thread.setDefaultUncaughtExceptionHandler(this); } @Override public void uncaughtException(Thread thread, Throwable ex) { // 捕获异常并重启应用程序 Intent intent = new Intent(getApplicationContext(), MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT); AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 1000, pendingIntent); System.exit(2); } } ``` 在上述示例代码中,我们创建了一个自定义的 Application 类,并实现 Thread.UncaughtExceptionHandler 接口。在 onCreate() 方法中,我们将当前线程的默认 UncaughtExceptionHandler 设置为该应用程序的 UncaughtExceptionHandler。 当应用程序中有未捕获异常时,会调用 uncaughtException() 方法。在该方法中,我们创建一个 Intent 对象,用于启动 MainActivity,然后使用 PendingIntent 将该 Intent 对象封装为一个闹钟事件,并在 1 秒钟后启动该事件。最后,我们调用 System.exit() 方法退出应用程序。 这样,当应用程序中发生未捕获异常时,应用程序将自动重启。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值