简体   繁体   English

警报“ onReceive”未调用

[英]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 我正在尝试创建一个警报,该警报将修复由于心跳错误而导致的与Google Cloud Messaging的丢失连接,请参见此处如何避免Android GCM消息中的延迟/更改心跳

However, my alarm's onReceive class which I have being set to be called every 1 minute for testing is never being called. 但是,我的警报的onReceive类(设置为每1分钟进行一次测试)从未被调用。 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 MainActivity.java

    //Alarm created here
    GCMHeartbeatAlarm gcmAlarm = new GCMHeartbeatAlarm();
    gcmAlarm.setAlarm(this);

GCMHeartbeatAlarm.java 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 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. 命中日志,但永远不会命中onReceive登录。

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. 我不太清楚为什么,但是使用System.currentTimeMillis()不能为triggerAtMillis值工作。 Perhaps it was because the alarm was set for ELAPSED_REALTIME instead of currentTimeMillis(), so the first alarm was never triggered. 可能是因为警报设置为ELAPSED_REALTIME而不是currentTimeMillis(),所以第一个警报从未触发。 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. 相反,我使用SystemClock.elapsedRealtime()+ 60 * 1000在设置警报后1分钟开始触发警报,然后以1分钟的间隔开始调用onReceive方法。

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!");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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