广播接收器无法接收闹钟广播

3
我有一段代码可以设置一个新的重复闹钟(在生产环境中我将使用inexactRepeating),但我注册用来处理它的广播接收器没有被调用。
这是我设置闹钟的代码:
newAlarmPeriod = 5000; // For debugging

Intent alarmIntent = new Intent(this, GroupsCheckAlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(this, Constants.CHECK_ALARM_CODE,
                                                  alarmIntent, 0);

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() 
                + newAlarmPeriod, newAlarmPeriod, sender);

看起来它能正常工作并且每五秒触发一次警报,如在“adb shell dumpsys alarm”输出中所见:

DUMP OF SERVICE alarm:
Current Alarm Manager state:

  Realtime wakeup (now=1269941046923):
  RTC_WAKEUP #1: Alarm{43cbac58 type 0 android}
    type=0 when=1269997200000 repeatInterval=0 count=0
    operation=PendingIntent{43bb1738: PendingIntentRecord{43bb1248 android broadcastIntent}}
  RTC_WAKEUP #0: Alarm{43ce30e0 type 0 com.almarsoft.GroundhogReader}
    type=0 when=1269941049555 repeatInterval=5000 count=1
    operation=PendingIntent{43d990c8: PendingIntentRecord{43d49108 com.almarsoft.GroundhogReader broadcastIntent}}
  RTC #1: Alarm{43bfc250 type 1 android}
    type=1 when=1269993600000 repeatInterval=0 count=0
    operation=PendingIntent{43c5a618: PendingIntentRecord{43c4f048 android broadcastIntent}}
  RTC #0: Alarm{43d67dd8 type 1 android}
    type=1 when=1269941100000 repeatInterval=0 count=0
    operation=PendingIntent{43c4e0f0: PendingIntentRecord{43c4f6c8 android broadcastIntent}}

  Broadcast ref count: 0

  Alarm Stats:
  android
    24390ms running, 0 wakeups
    80 alarms: act=android.intent.action.TIME_TICK flg=0x40000004
  com.almarsoft.GroundhogReader
    26ms running, 2 wakeups
    2 alarms: flg=0x4 cmp=com.almarsoft.GroundhogReader/.GroupsCheckAlarmReceiver

但是出现了某些问题,当闹钟被触发时,我的广播接收器没有被调用。我已经在清单文件中声明了它:

<receiver android:name=".GroupsCheckAlarmReceiver" />

这是缩写代码:

public class GroupsCheckAlarmReceiver extends BroadcastReceiver{
  @Override
  public void onReceive(Context context, Intent intent) {
     Toast.makeText(context, "XXX Alarm worked.", Toast.LENGTH_LONG).show();
     Log.d("XXX", "GroupsCheckAlarmReceiver.onReceive");
  }
2个回答

2

我找到了问题,它是一个非常愚蠢的错误。 <receiver> 标签在 <manifest> 中,但在 <application> 标签之外。感谢你们的时间。


0
你尝试过在清单接收器标志上明确设置“enabled”属性为true吗?我在文档中找不到任何说明,如果未指定默认值是什么。

1
我唯一的想法是,您可能希望在接收器标记内指定一个意图过滤器。但是从我所看到的情况来看,您不应该这样做,因为您的Intent已明确指向GroupCheckAlarmReceiver。 - Graeme Duncan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接