在UncaughtExceptionHandler中通过AlarmManager设置时不调用BroadcastReceiver

时间:2017-05-30 17:37:56

标签: android broadcastreceiver alarmmanager unhandled-exception

我使用IntentBroadcastReceiver在未捕获的异常期间为PendingIntent设置了AlarmManager,如下面的代码所示。在我的Android 7.0设备上,此代码正常运行。在早期的设备上,永远不会调用BroadcastReceiver。为什么不呢?

我的Thread.UncaughtExceptionHandlerApplication子类的成员:

public void handleUncaughtException (Thread thread, Throwable e)
{
  Intent restartIntent = new Intent (getApplicationContext (), RestartReceiver.class);

  PendingIntent pendingIntent =
    PendingIntent.getBroadcast (getApplicationContext (), 0, restartIntent, 0);

  AlarmManager alarm = (AlarmManager)getSystemService (Context.ALARM_SERVICE);
  alarm.set (AlarmManager.RTC, System.currentTimeMillis () + 5000, pendingIntent);

  System.exit (1);
}

接收者:

public class RestartReceiver extends BroadcastReceiver
{
  @Override
  public void onReceive (Context context, Intent intent)
  {
    Intent restartIntent = new Intent (context, RestartActivity.class);
    context.startActivity (restartIntent);
  }
}

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.perinote.crashtest"
          xmlns:android="http://schemas.android.com/apk/res/android">

  <application
    ...
    <receiver
      android:name=".RestartReceiver"
      android:enabled="true"
      android:exported="true" >
    </receiver>
  </application>

</manifest>

0 个答案:

没有答案