我使用Intent
和BroadcastReceiver
在未捕获的异常期间为PendingIntent
设置了AlarmManager
,如下面的代码所示。在我的Android 7.0设备上,此代码正常运行。在早期的设备上,永远不会调用BroadcastReceiver
。为什么不呢?
我的Thread.UncaughtExceptionHandler
,Application
子类的成员:
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>