Android异常捕获防止崩溃弹框
2016-01-05 13:47
465 查看
创建异常捕获handler类
import android.app.ActivityManager; import android.app.AlarmManager; import android.app.Application; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Looper; import android.widget.Toast; import com.magus.youxiclient.R; import com.magus.youxiclient.util.Log; /** * Created by yangshuai in the 10:53 of 2016.01.05 . */ public class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler { private static final String TAG = "ExceptionHandler"; private Thread.UncaughtExceptionHandler mUncaughtExceptionHandler; private Application mApplication; public MyUncaughtExceptionHandler(Application context) { mApplication = context; mUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler(); } @Override public void uncaughtException(Thread thread, Throwable ex) { if (mApplication.getPackageName().equals(getProcessName(mApplication))) { if (Log.showLog) { mUncaughtExceptionHandler.uncaughtException(thread, ex); } else { toastSorry(); restart(); android.os.Process.killProcess(android.os.Process.myPid()); } } else { android.os.Process.killProcess(android.os.Process.myPid()); } } private void toastSorry() { new Thread(){ @Override public void run() { Looper.prepare(); Toast.makeText( mApplication.getApplicationContext(), "Program is abnormal, is about to restart the application.", Toast.LENGTH_SHORT).show(); Looper.loop(); } }.start(); } public static String getProcessName(Context appContext) { String currentProcessName = ""; int pid = android.os.Process.myPid(); ActivityManager manager = (ActivityManager) appContext.getSystemService(Context.ACTIVITY_SERVICE); for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) { if (processInfo.pid == pid) { currentProcessName = processInfo.processName; break; } } return currentProcessName; } /* 重启应用 */ private void restart() { try { Thread.sleep(4000); } catch (InterruptedException e) { android.util.Log.e(TAG, "error : " + e); } Intent intent = new Intent( mApplication.getApplicationContext(), MainActivity.class); PendingIntent restartIntent = PendingIntent.getActivity( mApplication.getApplicationContext(), 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK); //退出程序 AlarmManager mgr = (AlarmManager) mApplication.getSystemService(Context.ALARM_SERVICE); mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, restartIntent); // 1秒钟后重启应用 } }
Application
import android.app.Application; import com.magus.trainingfirstapp.exception.CrashHandler; /** * Created by yangshuai on 2015/10/13 0013 11:01. */ public class MApplication extends Application { @Override public void onCreate() { super.onCreate(); /* 异常捕获(debug 时不捕获异常) */ Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler(this)); } }
相关文章推荐
- [Android分享] DialogFragment初次了解学习
- Android中Dialog的使用
- 用了Android studio半年,看了这篇文章,尼玛之前白用了
- Android JobScheduler/JobService 工作调度
- Android实现滑动刻度尺效果,选择身高体重和生日
- [Android分享] Android性能优化之常见的内存泄漏
- android实现大文件下载解析
- 从零開始学android<使用嵌套布局实现计算器界面.十七.>
- Unable to add window -- token android.os.BinderProxy@xxx is not valid; is your activity running
- Servies
- [Android算法] 解决ListView里嵌套CheckBox出现的数据刷新问题
- Android中使用Handler造成内存泄露的分析和解决
- Win10 下使用 ionic 框架开发 android 应用之搭载开发环境
- Android学习路线指南
- Android学习路线指南
- Android学习路线指南
- Windows环境下Android Studio系列—简单设置
- Android 倒计时文本框
- Windows环境下Android Studio系列—日志调试
- # Android Studio 最全的安装