WakefulBroadcastReceiver 没有收到来自 AlarmManager 的 PendingIntent
WakefulBroadcastReceiver doesn't receive PendingIntent from AlarmManager
我正在努力开发一个必须以指定间隔重复任务的应用程序。我希望它在需要时唤醒设备。我不知道为什么,但是 WakefulBroadcastReceiver 永远不会执行应该通过 AlarmManager 触发的 onReceive 方法。问题仍然存在于普通的 BroadcastReceiver。我使用的是 Lollipop 5.0.1 Nexus5,时间间隔为 5-20 秒。
代码如下:
class mypackage.MainActivity
[...]
private void startRepeatingAlarm(){
this.wbr = new SimpleWakefulBroadcastReceiver();
this.registerReceiver(wbr, new IntentFilter("mypackage.FOO_ACTION"));
Intent i = new Intent("mypackage.FOO_ACTION");
i.setClass(this, SimpleWakefulBroadcastReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 1234, i, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), timeInterval, pendingIntent);
Toast.makeText(this, "Alarm set", Toast.LENGTH_SHORT).show();
}
class mypackage.SimpleWakefulBroadcastReceiver
public class SimpleWakefulBroadcastReceiver extends WakefulBroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// This method gets NEVER called
Intent service = new Intent(context, SimpleWakefulService.class);
startWakefulService(context, service);
}}
我要在这里伸出我的脖子并猜测你没有在你的清单中声明广播接收器:
<receiver android:name=".SimpleWakefulBroadcastReceiver"/>
private void startRepeatingAlarm(){
Intent intent = new Intent(MainActivity.this, SimpleWakefulBroadcastReceiver.class);
intent.setAction(Intent.ACTION_MAIN);
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
0, intent, 0);
//repeat per 1 min
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1 * 60 * 1000, pendingIntent);
}
我正在努力开发一个必须以指定间隔重复任务的应用程序。我希望它在需要时唤醒设备。我不知道为什么,但是 WakefulBroadcastReceiver 永远不会执行应该通过 AlarmManager 触发的 onReceive 方法。问题仍然存在于普通的 BroadcastReceiver。我使用的是 Lollipop 5.0.1 Nexus5,时间间隔为 5-20 秒。
代码如下:
class mypackage.MainActivity
[...]
private void startRepeatingAlarm(){
this.wbr = new SimpleWakefulBroadcastReceiver();
this.registerReceiver(wbr, new IntentFilter("mypackage.FOO_ACTION"));
Intent i = new Intent("mypackage.FOO_ACTION");
i.setClass(this, SimpleWakefulBroadcastReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 1234, i, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), timeInterval, pendingIntent);
Toast.makeText(this, "Alarm set", Toast.LENGTH_SHORT).show();
}
class mypackage.SimpleWakefulBroadcastReceiver
public class SimpleWakefulBroadcastReceiver extends WakefulBroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// This method gets NEVER called
Intent service = new Intent(context, SimpleWakefulService.class);
startWakefulService(context, service);
}}
我要在这里伸出我的脖子并猜测你没有在你的清单中声明广播接收器:
<receiver android:name=".SimpleWakefulBroadcastReceiver"/>
private void startRepeatingAlarm(){
Intent intent = new Intent(MainActivity.this, SimpleWakefulBroadcastReceiver.class);
intent.setAction(Intent.ACTION_MAIN);
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
0, intent, 0);
//repeat per 1 min
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1 * 60 * 1000, pendingIntent);
}