Android拦截所有异常不让App崩溃的解决方法

public class CrashApp extends Application {

  public static final String TAG = "CrashApp";

  @Override
  protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
new Handler(getMainLooper()).post(new Runnable() { @Override public void run() { while (true) { try { Looper.loop();  //try-catch主线程的所有异常;Looper.loop()内部是一个死循环,出现异常时才会退出,所以这里使用while(true)。 } catch (Throwable e) { Log.d(TAG, "Looper.loop(): " + e.getMessage()); } } } }); Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) {  //try-catch子线程的所有异常。 Log.d(TAG, "UncaughtExceptionHandler: " + e.getMessage()); } });
} @Override
public void onCreate() { super.onCreate(); } }

<application android:name=".CrashApp">
</application>
 

 

posted @ 2019-04-11 16:17  yongfengnice  阅读(3875)  评论(0编辑  收藏  举报