简体   繁体   中英

Alarm “onReceive” not being called

I'm trying to create an Alarm that will fix lost connections to Google Cloud Messaging that occur due to the heartbeat bug, found here How to avoid delay in Android GCM messages / change heartbeat

However, my alarm's onReceive class which I have being set to be called every 1 minute for testing is never being called. I've looked in several of the other questions pertaining to this topic, and all of them concentrate on spelling and declaring the receiver in the manifest, which I've checked several times.

Here is all the 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 which is supposedly going to fix the GCM connection dropped bug, needs to be called every 5 mins or so via alarm to keep 
    //GCM connection open
    //Commented out for now
         // 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 this code, the "Alarm Set!" log is hit, but the log in onReceive is never hit.

Anything that could help me figure out what's going on would be greatly appreciated!

Got it to work eventually. I'm not quite sure why, but using System.currentTimeMillis() wasn't working for the triggerAtMillis value. Perhaps it was because the alarm was set for ELAPSED_REALTIME instead of currentTimeMillis(), so the first alarm was never triggered. Instead I used SystemClock.elapsedRealtime() + 60 * 1000 to trigger the alarm beginning 1 minute after it is set, and then the 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!");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM