【问题标题】:BroadcastReceiver not receiving an alarm's broadcastBroadcastReceiver 没有收到警报的广播
【发布时间】:2010-03-30 09:30:24
【问题描述】:

我有一个设置新重复警报的代码(在生产中我将使用 inexactRepeating),但我注册用于处理它的 BroadCastReceiver 没有被调用。

这是我设置闹钟的代码:

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

但由于某种原因,触发警报时没有调用我的 BroadCastReceiver。我已经在 Manifest 上声明了:

<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");
  }

【问题讨论】:

    标签: android


    【解决方案1】:

    我发现了问题,这是一个非常愚蠢的错误。 &lt;receiver&gt; 标签在 &lt;manifest&gt; 内,但在 &lt;application&gt; 标签外。谢谢你们的时间,伙计们。

    【讨论】:

      【解决方案2】:

      您是否尝试过将清单接收器标志上的“启用”属性显式设置为 true?如果未指定,我在文档中找不到任何说明默认值的内容。

      【讨论】:

      • 我唯一的另一个想法是你可能想在你的接收者标签中指定一个意图过滤器。但是,据我所知,您不应该 必须 这样做,因为您的 Intent 明确指向您的 GroupCheckAlarmReceiver。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-23
      • 2017-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多