首页 > 代码库 > APP崩溃重启

APP崩溃重启

测试了几种APP重启方式,还是这种方式比较靠谱;

public class App extends Application implements
Thread.UncaughtExceptionHandler{
Context context;
private PendingIntent intent; //APP退出后,PendingIntent还存在,设置2秒之后,重新启动你的主Activity
@Override
public void onCreate() {
super.onCreate();
context = this;
//设置Thread Exception Handler
Thread.setDefaultUncaughtExceptionHandler(this);
intent = PendingIntent.getActivity(this, 0,new Intent(this, MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
}

@Override
public void uncaughtException(Thread thread, Throwable ex) {
Log.e("TAG","uncaughtException");
AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 2000, intent);
System.exit(0);
}
 记住在manifest注册你的appliction;
}

APP崩溃重启