Android:如何在“强制closures”后自动重启应用程序?

在Android应用程序中,如果我们没有得到正确的例外,我们通常会得到“强制closures”的错误。

如果强制closures,我怎样才能自动重启我的应用程序?

有没有使用这个特定的权限?

要做到这一点,你必须做两件事:

  1. 避免“强制closures” – 应用程序崩溃的标准方式。
  2. 无论如何,崩溃发生时设置重启机制。

看下面如何做到这一点:

  1. 调用Thread.setDefaultUncaughtExceptionHandler()以捕获所有未捕获的exception,在这种情况下将调用uncaughtException()方法。 “强制closures”不会出现,应用程序将无响应,这不是一件好事。 为了在崩溃时重新启动应用程序,您应该执行以下操作:

  2. onCreate方法中,在您的主要活动中初始化一个PendingIntent成员:

    Intent intent = PendingIntent.getActivity( YourApplication.getInstance().getBaseContext(), 0, new Intent(getIntent()), getIntent().getFlags());

然后把下面的代码放在你的uncaughtException()方法中:

 AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE); mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 2000, intent); System.exit(2); 

您还必须调用System.exit() ,否则将无法正常工作。 这样你的应用程序将在2秒后重新启动。

最后,你可以设置一些标志,你的意图,应用程序崩溃,并在你的onCreate()方法,你可以显示一个对话框“我很抱歉,应用程序崩溃,希望永远不会再:)”。

诀窍是确保它不首先强制closures。

如果使用Thread.setDefaultUncaughtExceptionHandler()方法 ,则可以捕获导致应用程序强制closures的exception。

看看这个问题的例子,使用UncaughtExceptionHandler来logging应用程序引发的exception。

如果您使用Crittercism或其他错误报告服务,接受的答案几乎是正确的..

 final UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler(); Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() { public void uncaughtException(Thread thread, Throwable ex) { Intent launchIntent = new Intent(activity().getIntent()); PendingIntent pending = PendingIntent.getActivity(CSApplication.getContext(), 0, launchIntent, activity().getIntent().getFlags()); getAlarmManager().set(AlarmManager.RTC, System.currentTimeMillis() + 2000, pending); defaultHandler.uncaughtException(thread, ex); } }); 
 public class ForceCloseExceptionHandalingActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setContentView(MyLayout()); Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread paramThread, Throwable paramThrowable) { myHandaling(paramThread, paramThrowable); } }); } private ViewGroup MyLayout(){ LinearLayout mainLayout = new LinearLayout(this); mainLayout.setOrientation(LinearLayout.VERTICAL); Button btnHello =new Button(this); btnHello.setText("Show all button"); btnHello.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { setContentView(MyLayout2()); } }); mainLayout.addView(btnHello); return mainLayout; } private ViewGroup MyLayout2(){ LinearLayout mainLayout = new LinearLayout(this); mainLayout.setOrientation(LinearLayout.VERTICAL); Button btnHello =new Button(this); btnHello.setText("I am a EEROR uncaughtException"); btnHello.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Log.e("Alert","btn uncaughtException::"); Toast.makeText(ForceCloseExceptionHandalingActivity.this, "Alert uncaughtException222",Toast.LENGTH_LONG).show(); View buttone = null; setContentView(buttone); } }); Button btnHello2 =new Button(this); btnHello2.setText("I am a EEROR Try n catch"); btnHello2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try{ View buttone = null; setContentView(buttone); } catch (Exception e) { Log.e("Alert","Try n catch:::"); Toast.makeText(ForceCloseExceptionHandalingActivity.this, "Alert Try n catch",Toast.LENGTH_LONG).show(); setContentView(MyLayout()); } } }); mainLayout.addView(btnHello); mainLayout.addView(btnHello2); return mainLayout; } public void myHandaling(Thread paramThread, Throwable paramThrowable){ Log.e("Alert","Lets See if it Works !!!" +"paramThread:::" +paramThread +"paramThrowable:::" +paramThrowable); Toast.makeText(ForceCloseExceptionHandalingActivity.this, "Alert uncaughtException111",Toast.LENGTH_LONG).show(); Intent in =new Intent(ForceCloseExceptionHandalingActivity.this,com.satya.ForceCloseExceptionHandaling.ForceCloseExceptionHandalingActivity.class); startActivity(in); finish(); android.os.Process.killProcess(android.os.Process.myPid()); } @Override protected void onDestroy() { Log.e("Alert","onDestroy:::"); Toast.makeText(ForceCloseExceptionHandalingActivity.this, "Alert onDestroy",Toast.LENGTH_LONG).show(); super.onDestroy(); }