【问题标题】:BroadcastReceiver not being called?BroadcastReceiver 没有被调用?
【发布时间】:2014-04-19 21:17:17
【问题描述】:

我在启动活动中使用 Alarmmanager 重复执行 BroadcastReceiver 中的某些内容,但 BroadcastReceiver 没有被触发。我的代码如下:

MainActivity.class:

AlarmManager manager = (AlarmManager) getBaseContext().getSystemService(Context.ALARM_SERVICE);
Intent gpsintent =new Intent(getBaseContext(),gps_sendback_alarm_receiver.class);
PendingIntent.getService(getBaseContext(), 0, gpsintent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar now = Calendar.getInstance();
manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, now.getTimeInMillis(), int_gps_sendback_period, pendingIntent);

gps_sendback_alarm_receiver.class:

public void onReceive(Context context, Intent intent) {
//send back the GPS location
}

清单:

<application>
<receiver android:name="com.example.xmobiler.gps_sendback_alarm_receiver" android:enabled="true"/>
</application>

有人可以帮忙吗?

【问题讨论】:

    标签: android broadcastreceiver alarmmanager


    【解决方案1】:

    您正在创建变量 gpsintent,但警报管理器获得了您的示例代码中未分配的 pendingIntent

    【讨论】:

    • 我查看logcat,我看到AlarmManager被触发了,但是gps_sendback_alarm_receiver没有,为什么? V/AlarmManager(2276):waitForAlarm 结果:4 V/AlarmManager(2276):触发 ELAPSED_REALTIME_WAKEUP 或 RTC_WAKEUP
    • 我使用“adb shell dumpsys alarm”查看是否注册了AlarmManager.ELAPSED_REALTIME_WAKEUP,答案是没有。为什么logcat的登录和adb不一样?
    • 你能告诉我们定义和使用变量pendingIntent的代码吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-22
    • 1970-01-01
    相关资源
    最近更新 更多