Android异常捕获防止崩溃弹框
16lz
2021-12-04
创建异常捕获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(安卓)apps应用检查更新代码
- android正确使用killProcess完全退出应用
- android TabLayout的应用
- android手机端保存xml数据
- Android(安卓)Training - 使用IntentService执行任务(Lesson 1 -
- Android之OenGL ES 之 EGL
- Android中带分割线的九宫格
- android 应用程序性能跟踪
- Android(安卓)JNI编程