에이치의 모바일 앱 개발
uncaughtexception version 1.0.2 본문
manifast setting version 1.0.2
<application
android:name=".uncaughtexception.UnCaughtExceptionApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
uncaughtexception application version 1.0.2
import android.app.Application;
import android.content.Intent;
import android.os.Looper;
import android.widget.Toast;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.Thread.UncaughtExceptionHandler;
/**
* Created by lsh on 2016-08-03.* version 1.0.2
*/
public class UnCaughtExceptionApplication extends Application {
private void showToast() {
new Thread() {
@Override
public void run() {
// UI쓰레드에서 토스트 뿌림
Looper.prepare();
Toast.makeText(getApplicationContext(), "에러메시지", Toast.LENGTH_SHORT)
.show();
Looper.loop();
}
}.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
private void showDialog(String errorMessage) {
Intent crashedIntent = new Intent(getApplicationContext(), ErrorMessageDialogActivity.class);
crashedIntent.putExtra("Message", errorMessage);
crashedIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(crashedIntent);
if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
}
//===============================================================================
// uncaught exception handler variable
private final UncaughtExceptionHandler defaultUEH;
public UnCaughtExceptionApplication() {
defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
// setup handler for uncaught exception
Thread.setDefaultUncaughtExceptionHandler(_unCaughtExceptionHandler);
}
// handler listener
private final UncaughtExceptionHandler _unCaughtExceptionHandler = new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
showDialog(getStackTrace(ex));
// re-throw critical exception further to the os (important)
// defaultUEH.uncaughtException(thread, ex);
}
};
public static String getStackTrace(Throwable th) {
final Writer result = new StringWriter();
final PrintWriter printWriter = new PrintWriter(result);
Throwable cause = th;
while (cause != null) {
cause.printStackTrace(printWriter);
cause = cause.getCause();
}
final String stacktraceAsString = result.toString();
printWriter.close();
return stacktraceAsString;
}
}
'Android > Android 개발 소스' 카테고리의 다른 글
restart application version 1.0.1 (0) | 2017.12.23 |
---|---|
error message dialog activity version 1.0.1 (0) | 2017.12.23 |
File & I/O (0) | 2017.12.23 |
DBunzip version 1.0.1 (0) | 2017.12.23 |
DBFileDownload version 1.0.1 (0) | 2017.12.23 |
Comments