【发布时间】:2016-03-13 07:38:59
【问题描述】:
我想使用 AlarmManager 类的 setAlarmClock 方法启动警报,但这不起作用,我的图标出现在状态栏中,当它是时间警报时消失,但我的广播接收器没有根本火。
我的代码是这样的:
if(versionOfAndroid >= Build.VERSION_CODES.LOLLIPOP) {
AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(calendar.getTimeInMillis(), pendingIntent);
alarmManager.setAlarmClock(alarmClockInfo, pendingIntent);
}
我的广播接收器是这样的:
private class SelfWakeUpReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_WAKE_UP_ALARM)) {
Intent intent1 = new Intent(context, WakeUpActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
context.startActivity(intent1);
}
我不明白,图标在警报时出现和消失,但没有其他反应。这个应用程序可以在除三星以外的其他设备上与他们的智能经理一起在 kitkat 上正常运行...
我的pendingIntent是这样的:
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, numeroAlarm, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
和我在 onCreate() 方法中的 Intent 类似:
broadcastIntent = new Intent(this, SelfWakeUpReceiver.class);
broadcastIntent.setAction(ACTION_WAKE_UP_ALARM);
我这样注册接收器:
intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED);
intentFilter.addAction(BluetoothLeService.ACTION_GATT_DISCONNECTED);
intentFilter.addAction(WakeUpActivity.ACTION_STOP_ALARM);
intentFilter.addAction(ACTION_WAKE_UP_ALARM);
registerReceiver(mSelfWakeUpReceiver, intentFilter);
【问题讨论】:
-
你是如何创建
pendingIntent的?您是否添加了任何日志记录以查看您是否获得了控制权但没有进入您的if块?闹钟事件发生的时间点是否有有趣的 LogCat 消息? -
@CommonsWare 感谢您的帮助,我正在编辑我的帖子。是的,我在 if{} 语句中获得了控制权(我在 if{} 语句中删除了我的 log.i(...)。当我启动警报时,我在 Logcat 中有以下信息:Activity_idle id:android.os。 BinderProxy@843990e 时间:122065797 。但没有别的,当触发警报时,只有状态栏中的图标消失。
-
“是的,我正在控制我的 if{} 语句”——我现在看到您有两个
if语句。我指的是SelfWakeUpReceiver中的那个。你在那里得到控制?如果是,那么问题是设备不想开始您的活动。如果不是,那么问题是设备不想发送广播,或者您的PendingIntent以某种方式被取消了。 -
@CommonsWare 哦,不,我无法控制接收器中的 if{} 语句...我不明白为什么?我在我的摩托罗拉设备 (kitkat) 中进行了测试,但它也不起作用,所以我的接收器是问题所在。我在 onCreate() 方法中注册我的接收器(见编辑)
-
我从未尝试使用通过
registerReceiver()注册的接收者setAlarmClock()。这不太可能正常工作,因为您的进程可能会在调用setAlarmClock()和事件发生的时间之间终止。我建议您在清单中注册该接收器。
标签: android alarmmanager android-alarms