android - Alarm "onReceive" not being called -
i'm trying create alarm fix lost connections google cloud messaging occur due heartbeat bug, found here how avoid delay in android gcm messages / change heartbeat
however, alarm's onreceive class have being set called every 1 minute testing never being called. i've looked in several of other questions pertaining topic, , of them concentrate on spelling , declaring receiver in manifest, i've checked several times.
here relevant code:
mainactivity.java
//alarm created here gcmheartbeatalarm gcmalarm = new gcmheartbeatalarm(); gcmalarm.setalarm(this);
gcmheartbeatalarm.java
public class gcmheartbeatalarm extends broadcastreceiver { private alarmmanager alarmmgr; private pendingintent alarmintent; @override public void onreceive(context context, intent intent) { //the part supposedly going fix gcm connection dropped bug, needs called every 5 mins or via alarm keep //gcm connection open //commented out // context.sendbroadcast(new intent("com.google.android.intent.action.gtalk_heartbeat")); // context.sendbroadcast(new intent("com.google.android.intent.action.mcs_heartbeat")); log.i("gcmheartbeat", "gcm heartbeat sent!"); } public void setalarm(context context) { alarmmgr=(alarmmanager)context.getsystemservice(context.alarm_service); intent intent = new intent(context, gcmheartbeatalarm.class); alarmintent = pendingintent.getbroadcast(context, 0, intent, 0); //repeat every 1 minute alarmmgr.setrepeating(alarmmanager.elapsed_realtime, system.currenttimemillis(), 1000 * 60 * 1 , alarmintent); log.i("gcmheartbeat", "alarm set!"); } }
androidmanifest.xml
<!-- gcm heartbeat alarm receiver --> <receiver android:name="com.myapp.app.gcmheartbeatalarm"> </receiver>
with code, "alarm set!" log hit, log in onreceive never hit.
anything me figure out what's going on appreciated!
got work eventually. i'm not quite sure why, using system.currenttimemillis() wasn't working triggeratmillis value. perhaps because alarm set elapsed_realtime instead of currenttimemillis(), first alarm never triggered. instead used systemclock.elapsedrealtime() + 60 * 1000 trigger alarm beginning 1 minute after set, , onreceive method started being called in 1 minute intervals.
the working code was:
public void setalarm(context context) { alarmmgr = (alarmmanager)context.getsystemservice(context.alarm_service); intent intent = new intent(context, gcmheartbeatalarm.class); alarmintent = pendingintent.getbroadcast(context, 0, intent, 0); alarmmgr.setrepeating(alarmmanager.elapsed_realtime, systemclock.elapsedrealtime() + 60 * 1000, 60 * 1000, alarmintent); log.e("gcmheartbeat", "alarm set!"); }
Comments
Post a Comment