广播接收器没有收到报警的广播 [英] BroadcastReceiver not receiving an alarm's broadcast

查看:144
本文介绍了广播接收器没有收到报警的广播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个code,设置新的重复报警(生产我将使用一个inexactRepeating),但我已经注册为处理它没有被称为广播接收器。

下面是code,我设置报警:

  newAlarmPeriod = 5000; //为了调试意图alarmIntent =新意图(这一点,GroupsCheckAlarmReceiver.class);
发件人的PendingIntent = PendingIntent.getBroadcast(这一点,Constants.CHECK_ALARM_ code,
                                                  alarmIntent,0);AlarmManager AM =(AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis的()
                + newAlarmPeriod,newAlarmPeriod,发件人);

这似乎工作,并触发报警每五秒钟,如在的亚行外壳dumpsys报警的输出:

 的报警服务的DUMP:
当前报警管理状态:  实时唤醒(现= 1269941046923):
  RTC_WAKEUP#1:报警{43cbac58类型0的Andr​​oid}
    类型= 0时= 1269997200000按repeatInterval = 0计数= 0
    操作= {的PendingIntent 43bb1738:PendingIntentRecord {43bb1248安卓broadcastIntent}}
  RTC_WAKEUP#0:报警{43ce30e0类型0 com.almarsoft.GroundhogReader}
    类型= 0时= 1269941049555按repeatInterval = 5000计数= 1
    操作= {的PendingIntent 43d990c8:PendingIntentRecord {43d49108 com.almarsoft.GroundhogReader broadcastIntent}}
  RTC#1:报警{43bfc250 1型机器人}
    类型= 1时,= 1269993600000按repeatInterval = 0计数= 0
    操作= {的PendingIntent 43c5a618:PendingIntentRecord {43c4f048安卓broadcastIntent}}
  RTC#0:报警{43d67dd8 1型机器人}
    类型= 1时,= 1269941100000按repeatInterval = 0计数= 0
    操作= {的PendingIntent 43c4e0f0:PendingIntentRecord {43c4f6c8安卓broadcastIntent}}  广播引用计数:0  报警统计:
  安卓
    24390ms运行,0唤醒
    80个报警:ACT = android.intent.action.TIME_TICK FLG = 0x40000004
  com.almarsoft.GroundhogReader
    26ms运行,2唤醒
    2报警:FLG =为0x4 = CMP com.almarsoft.GroundhogReader / .GroupsCheckAlarmReceiver

但出于某种原因触发报警,当我的广播接收器不会被调用。我已经声明它放在清单:

 <接收机器人:GroupsCheckAlarmReceiverNAME = />

这是简写code:

 公共类GroupsCheckAlarmReceiver扩展广播接收器{
  @覆盖
  公共无效的onReceive(上下文的背景下,意图意图){
     Toast.makeText(背景下,XXX报警工作。Toast.LENGTH_LONG).show();
     Log.d(XXX,GroupsCheckAlarmReceiver.onReceive);
  }


解决方案

我发现这个问题,这是一个pretty愚蠢的错误。在<接收> 标记是在<清单> &LT外;用途> 标记。感谢您的时间,伙计们。

I have a code that sets a new repeating alarm (on production I'll use a inexactRepeating), but the BroadCastReceiver I've registered for handling it is not being called.

Here is the code where I set the alarm:

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

It seems to work and it triggers and alarm every five seconds, as seen in the output of "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

But for some reason my BroadCastReceiver is not being called when the alarm is triggered. I've declared it on the Manifest:

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

And this is the abbreviated code:

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

解决方案

I found the problem, and it was a pretty stupid error. The <receiver> tag was inside the <manifest> but outside the <application> tags. Thanks for your time, guys.

这篇关于广播接收器没有收到报警的广播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆