public class BaseApplication extends Application implements
UncaughtExceptionHandler {
private static Context context;
private PendingIntent restartIntent;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
BaseApplication.context = getApplicationContext();
Thread.setDefaultUncaughtExceptionHandler(this);
init();
}
private void init() {
Intent intent = new Intent(getApplicationContext(), ExceptionActivity.class);
restartIntent = PendingIntent.getActivity(getApplicationContext(), 0,
intent, Intent.FLAG_ACTIVITY_NEW_TASK);
}
public static Context getContext() {
return context;
}
public static void startActivity(Class<?> activity) {
Intent intent = new Intent(context, activity);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
// TODO Auto-generated method stub
System.out.println("uncaughtException");
AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 500,
restartIntent); // 0.5钟后重启应用的指定Activity
System.exit(2);//结束当前遇到线程出错的应用。
}
}
11-11