需要长时间运行的androidapp,需要碰到异常时候能够自己自动重启动
1.继承接口
public class MainActivity extends ActionBarActivity implements Thread.UncaughtExceptionHandler{
2.在oncreate添加代码
Thread.setDefaultUncaughtExceptionHandler(this);
3.实现接口,启动自身
@Override
public void uncaughtException(Thread thread, Throwable ex) {
//System.out.println("uncaughtException");
Log.i("mylog", "restart.......................");
//
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
System.exit(0);
}