2010-03-30 103 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