我想顺利处理崩溃,因为我正在为每个未捕获的异常重新启动应用程序但是总是有警告框“不幸的是,xyz已停止工作” 我用来处理未捕获的异常的代码:
intent = PendingIntent.getActivity(this.getApplication().getBaseContext(), 0,
new Intent(getIntent()), getIntent().getFlags());
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread paramThread,
Throwable paramThrowable) {
Log.e("Alert", "Lets See if it Works !!!");
AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 2000, intent);
System.exit(0);
}
答案 0 :(得分:1)
当你“不幸的是,xyz已经停止工作”时,总是放置一些断点并正确调试你的代码。检查你的LOGCAT,当你的应用程序崩溃时,你会发现你遇到的问题。 谢谢!
答案 1 :(得分:1)
代码:
public class BaseActivity extends Activity {
@Override
public void onCreate() {
super.onCreate();
final Thread.UncaughtExceptionHandler oldHandler =
Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(
new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(
Thread paramThread,
Throwable paramThrowable
) {
//Do your own error handling here
if (oldHandler != null)
oldHandler.uncaughtException(
paramThread,
paramThrowable
); //Delegates to Android's error handling
else
System.exit(2); //Prevents the service/app from freezing
}
});
}
}
答案 2 :(得分:0)
警报由系统完成,你无能为力。
您应该在抛出异常时捕获异常,然后让应用程序的流程继续相应。每次发生异常时重新启动应用程序都是顺利的。
答案 3 :(得分:0)
@Override
public void uncaughtException(Thread paramThread,
Throwable paramThrowable) {
Log.e("Alert", "Restarting app !!!");
Intent intent = getBaseContext().getPackageManager()
.getLaunchIntentForPackage( getBaseContext().getPac
//i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTI
startActivity(intent);
System.exit(2);
}
});