2

我正在使用AlarmManager進行調度。我的鬧鐘已設置。但是這個警報不會調用BroadcastReceiver來捕捉事件。我已經搜索了很多,但我還沒有找到任何解決問題的方法。我張貼我的代碼,請看一看,看看我是否缺少一些東西。報警管理器沒有激活廣播接收器?

AlarmManagerClass:

public class ScheduleMessageManager { 

Context context; 
PendingIntent sender; 
AlarmManager am; 


public ScheduleMessageManager(Context context) { 
    this.context = context; 
} 

public void addAlram(int scheduledMessageID, long scheduledTime) { 

    // Activate Broadcast Receiver to receive broadcasts 
    activateBroadcastReceiver(); 
    //Calendar cal = Calendar.getInstance(); 

    Intent intent = new Intent(context, AlarmReceiver.class); 
    // In reality, you would want to have a unique variable for the request 
    // code 
    intent.putExtra("scheduledMessageID", scheduledMessageID); 
    sender = PendingIntent.getBroadcast(context, scheduledMessageID, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

    // Get the AlarmManager service 
    am = (AlarmManager) context.getSystemService(context.ALARM_SERVICE); 
    am.set(AlarmManager.RTC_WAKEUP, scheduledTime, sender); 

    Log.e("In ScheduleMessageManage", "***** Alarm is set to the mmessage *****"); 
} 

public void cancelPeriodicSchedule(PendingIntent sender) { 
    if (am != null) { 
     if (sender != null) { 
      am.cancel(sender); 
      sender.cancel(); 
     } 
    } 

    // Deactivate Broadcast Receiver to stop receiving broadcasts 
    deactivateBroadcastreceiver(); 
} 

private void activateBroadcastReceiver() { 
    PackageManager pm = context.getPackageManager(); 
    ComponentName componentName = new ComponentName(context, AlarmReceiver.class); 
    pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); 
    Toast.makeText(context, "activated", Toast.LENGTH_LONG).show(); 
} 

private void deactivateBroadcastreceiver() { 
    // TODO Auto-generated method stub 

    PackageManager pm = context.getPackageManager(); 
    ComponentName componentName = new ComponentName(context, AlarmReceiver.class); 
    pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); 
    Toast.makeText(context, "cancelled", Toast.LENGTH_LONG).show(); 

} 

}

我AlarmReceiver:

public class AlarmReceiver extends BroadcastReceiver { 

int pendingIntentID; // same as scheduledMessageID 

@Override 
public void onReceive(Context context, Intent intent) { 

    Log.e("In On Receive", "Alarm has Initiated Broadcast Receiver...."); 

    if (intent.hasExtra("scheduledMessageID")) { 
     pendingIntentID = intent.getExtras().getInt("scheduledMessageID"); 
     Intent sendMessageServiceIntent = new Intent(context, SendMessageService.class); 
     sendMessageServiceIntent.putExtra("pendingIntentID", pendingIntentID); 
     context.startService(sendMessageServiceIntent); 
    } 
} 

}

OnReceieve()是永遠調用。

在我的Manifest.xml

<receiver 
     android:name="myPackage.AlarmReceiver" 
     android:enabled="true" > 
    </receiver> 

我無法找出確實的問題出在哪裏。請幫我解決它。謝謝。!

+0

你檢查的時間,是正確的?你是否在使用GregorianCalendar – 2013-03-07 09:42:23

+0

是的,我認爲時間是正確的,因爲我也將它顯示在我的清單上,顯示正確的時間。 – 2013-03-07 09:43:46

+0

聽起來很愚蠢,但你有沒有檢查過scheduleTime是未來? (這是觸發報警的絕對時間,而不是報警發生前經過的時間)。對不起,如果你知道這一點,並已檢查。 – 2013-03-07 09:45:03

回答

4

http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html中給出了關於使用廣播接收器的一個很好的教程。實質上,您的接收器不會聲明它將接收的事件。在清單文件中的申報需要的東西,如:

<receiver 
    android:name="myPackage.AlarmReceiver" 
    android:enabled="true" > 
    <intent-filter> 
      <action android:name="your.company.blah.mybroadcast" /> 
     </intent-filter> 
</receiver> 

而且當你創建的意圖,它需要

Intent intent = new Intent(); 
intent.setAction("your.company.blah.mybroadcast"); 
// All the other things you want to put in the intent 
+0

@尼爾......這是一個與時間有關的問題。這並沒有在一個地方得到更新。 – 2013-03-07 12:31:44

+0

謝謝,很高興知道。 – 2013-03-07 12:36:58

+0

我面臨同樣的問題,所以請@UsamaSarwar告訴我什麼是解決方案;它與timzone有關嗎? – Amt87 2014-02-17 10:57:28

2

我知道,這已經被回答,只是進一步的參考,因爲我跑到同一個問題,確保你的接收器標籤在標籤內,但不是在任何其他標籤,如活動(這正是我的問題)。

此外,它幫了不少忙,檢查您的報警已經註冊成功,就這麼跑: 亞行外殼dumpsys報警

-1

你只需要註冊您的接收器與報警經理,這將每60 CALL後播出第二:

Intent broadcastIntent = new Intent(YourActivity.this, AlarmReceiver.class); 

     PendingIntent pi = PendingIntent.getBroadcast(this, 0, broadcastIntent, 0); 

     AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 

     long timeInterval = 60 * 1000; 

     am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), timeInterval, pi); 

在清單:

<receiver 
     android:name="myPackage.AlarmReceiver" 
     android:enabled="true" > 
    </receiver>