Created
February 6, 2016 17:30
-
-
Save fredgrott/74ab1043f6ec2d4d895d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ASL2.0 Copyright(C) 2016 Fred Grott | |
public class MyApplication extends Application { | |
public void onCreate () | |
{ | |
Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler() | |
{ | |
@Override | |
public void uncaughtException (Thread thread, Throwable e) | |
{ | |
handleUncaughtException (thread, e); | |
} | |
}); | |
} | |
public boolean isUIThread(){ | |
return Looper.getMainLooper().getThread() == Thread.currentThread(); | |
} | |
/** | |
* If you are using a 3rd party analytics and logging than you might | |
* want to put those hooks to sending log file in this method | |
*/ | |
private void handleUncaughtException (Thread thread, Throwable e){ | |
e.printStackTrace(); // not all Android versions will print the stack trace automatically | |
if(isUIThread()) { | |
//invokeLogActivity(); | |
}else{ //handle non UI thread throw uncaught exception | |
//new Handler(Looper.getMainLooper()).post(new Runnable() { | |
// @Override | |
// public void run() { | |
// invokeLogActivity(); | |
//} | |
//}); | |
} | |
} | |
private void invokeLogActivity(){ | |
Intent intent = new Intent (); | |
intent.setAction ("com.mydomain.SEND_LOG"); | |
intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK); | |
startActivity (intent); | |
System.exit(1); // kill off the crashed app | |
} | |
} | |
and in the manifest, if you are using the invokeLogActivity option: | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" ... > | |
<!-- needed for Android 4.0.x and eariler --> | |
<uses-permission android:name="android.permission.READ_LOGS" /> | |
<application ... > | |
<activity | |
android:name="com.mydomain.SendLog" | |
android:theme="@android:style/Theme.Dialog" | |
android:textAppearance="@android:style/TextAppearance.Large" | |
android:windowSoftInputMode="stateHidden"> | |
<intent-filter> | |
<action android:name="com.mydomain.SEND_LOG" /> | |
<category android:name="android.intent.category.DEFAULT" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment