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>