2010-03-30 75 views
2

我有一个代码,设置一个新的重复警报(在生产中我将使用一个不精确的重复),但我已经注册处理它的BroadCastReceiver没有被调用。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); 

看来工作,并触发报警每五秒钟,如在的“亚行外壳dumpsys报警”输出:

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未被调用。我已经声明它的清单:

<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

我发现这个问题,这是一个非常愚蠢的错误。 <receiver>标记位于<manifest>之内,但位于<application>标记之外。谢谢你的时间,伙计们。

0

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

+0

刚刚尝试过。接收器仍然没有被调用。 – juanjux 2010-03-30 09:45:42

+1

我唯一的想法是,你可能想在你的接收器标签中指定一个意图过滤器。但是从我所看到的你不应*有*做到这一点,因为你的意图明确指向你的GroupCheckAlarmReceiver。 – 2010-03-30 09:55:08