android thread 捕获异常,Android开发全局异常捕获

释放双眼,带上耳机,听听看~!

使用Android提供的全局异常捕获的方法

Thread.setDefaultUncaughtExceptionHandler(this);

它的意思是设置默认未捕获的异常处理handler,只要你的程序发生了异常,没有被处理,就会调用它来处理异常。

/**

* 全局异常捕获

*/

public class CrashHandler implements Thread.UncaughtExceptionHandler {

/**

* 系统默认UncaughtExceptionHandler

*/

private Thread.UncaughtExceptionHandler mDefaultHandler;

/**

* context

*/

private Myapplicaition applicaition;

/**

* 获取CrashHandler实例

*/

public static synchronized CrashHandler getInstance(){

if(null == mInstance){

mInstance = new CrashHandler();

}

return mInstance;

}

public void init(Myapplicaition applicaition){

this.applicaition = applicaition;

mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();

//设置该CrashHandler为系统默认的

Thread.setDefaultUncaughtExceptionHandler(this);

}

/**

* uncaughtException 回调函数

*/

@Override

public void uncaughtException(Thread thread, Throwable ex) {

Log.d("uncaughtException",ex.getMessage());

if(!handleException(ex) && mDefaultHandler != null){//如果自己没处理交给系统处理

mDefaultHandler.uncaughtException(thread,ex);

}else{

//自己处理

}

}

然后在继承application类中,实例化CrashHandler,并调用init函数

public class Myapplicaition extends Application {

@Override

public void onCreate() {CrashHandler.getInstance().init(this);//初始化全局异常管理}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android应用中,可以通过全局捕获异常来避免应用闪退。可以通过以下步骤实现: 1. 创建一个自定义的Application类,并在其中重写`Thread.UncaughtExceptionHandler`接口的`uncaughtException`方法。 2. 在`uncaughtException`方法中处理全局异常,例如记录异常信息、上传日志或者进行其他处理操作。 3. 在Application的onCreate方法中,将自定义的UncaughtExceptionHandler设置为默认的异常处理器。 下面是一个示例代码: ```java 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) { // 处理全局异常,例如记录异常信息、上传日志等操作 Log.e("MyApplication", "Uncaught Exception: " + ex.getMessage()); // 重启应用或者执行其他操作 restartApp(); } private void restartApp() { // 重启应用,可以根据实际需求来实现 Intent intent = new Intent(getApplicationContext(), MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT); AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, pendingIntent); // 退出应用 System.exit(0); } } ``` 记得在AndroidManifest.xml文件中将自定义的Application类配置为应用的默认Application类: ```xml <application android:name=".MyApplication" ...> ... </application> ``` 通过以上步骤,当应用发生未捕获异常时,会调用自定义的异常处理方法,你可以在其中进行相应的处理操作,例如记录异常信息、上传日志等。最后,你可以选择重启应用或者执行其他操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值