Sunday, 15 August 2010

android - BroadcastReceiver from AlarmManager not getting called -



android - BroadcastReceiver from AlarmManager not getting called -

i have class extends intentservice. stuff in class called when app reboots (and on app startup). loads settings , stuff in background (it's productivity tool/utility).

this intentservice, when loading my, let's phone call "stuff", decides whether or not add together alarms via alarmmanager. have broadcastreceiver receives different parameters , method based on parameters. here code in scheduling alarms:

public void addalarm(int id, int hourfrom, int hourto, int minutefrom, int minuteto) { alarmmanager alarmmanager = (alarmmanager) getbasecontext().getsystemservice(context.alarm_service); // enable intent intent = new intent(controller.key_controller_action); intent.putextra(controller.key_controller_action, controller.key_action_enable); intent.putextra(controller.key_controller_id, id); pendingintent pendingintent = pendingintent.getbroadcast( this, id, intent, pendingintent.flag_update_current); calendar calendar = calendar.getinstance(); calendar.set(calendar.hour_of_day, hourfrom); calendar.set(calendar.minute, minutefrom); calendar.set(calendar.second, 00); int hours_24 = 24 * 60 * 60 * 1000; alarmmanager.setrepeating(alarmmanager.rtc_wakeup, calendar.gettimeinmillis(), hours_24, pendingintent); // cancel intent cancelintent = new intent(controller.key_controller_action); intent.putextra(controller.key_controller_action, controller.key_action_end); intent.putextra(controller.key_controller_id, id); pendingintent cancelpendingintent = pendingintent.getbroadcast( this, id, cancelintent, pendingintent.flag_update_current); calendar.set(calendar.hour_of_day, hourto); calendar.set(calendar.minute, minuteto); calendar.set(calendar.second, 00); alarmmanager.setrepeating(alarmmanager.rtc_wakeup, calendar.gettimeinmillis(), hours_24, cancelpendingintent);

here's problem: broadcastreceiver (controller class) sometimes has onreceive called. when does, it's few minutes delayed (no big deal; problem many times never gets called). second alarm (cancelintent) never gets called.

my controller broadcastreceiver declared manifest. suspect has both intents beingness identical (both have extras type of action, id).

what doing wrong? alternatively, more proper way go have from-to based alarm? this, mean need method happen @ time, , different method (in same class; controller broadcastreceiver) @ "end" time.

i modified manifest take multiple action (real actions, rather pseudo-actions doing via addextra).

<receiver android:name="com.mycompany.myapp.controller"> <intent-filter> <action android:name="com.mycompany.myapp.controller.start"/> </intent-filter> <intent-filter> <action android:name="com.mycompany.myapp.controller.end" /> </intent-filter> </receiver>

then, when add together alarms intent, create sure phone call setaction, specifying string action. in onreceive of broadcastreceiver phone call getaction() (returns string). , voila! both alarms fire off intended, , can type of action , perform associated methods.

note: answered help of @chitrang (via comments question). pointed out intent getting canceled because there alarm scheduled intentsender.

android service broadcastreceiver alarmmanager intentservice

No comments:

Post a Comment