我发现Android O上的某些内容有问题。我已经完成了Reminder
功能的实现。我想在固定的时间发送广播。 AlarmManager正常工作,BroadcastReceiver执行onReceive
但意图没有数据。我检查了一切,而我设置的警报数据附加到意图。我认为在过去它运作良好,同时发生了一些事情。以下代码显示了我如何设置Intent
,PendingIntent
和闹钟。
Intent(context, AlarmReceiver::class.java).apply {
action = System.currentTimeMillis().toString()
putExtra(AlarmService.ID_KEY, alarmId)
putExtra(AlarmService.CONTENT_TASK, task)
putExtra(AlarmService.CONTENT_ID, task.localId.value)
putExtra(AlarmService.CONTENT_STATUS, task.status)
putExtra(AlarmService.CONTENT_TITLE_KEY, notificationTitle)
putExtra(AlarmService.MESSAGE_KEY, notificationMessage)
}
待定内网:
private fun createPendingIntent(context: Context, intent: Intent, alarmId: Int): PendingIntent {
return PendingIntent.getBroadcast(context, alarmId, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
报警:
alarmManager.set(AlarmManager.RTC_WAKEUP, AppGlobal.getTimeToMilisecond(task.date) - timeAgo, createPendingIntent(context, intent, alarmId))
我还尝试通过将BroadcastReceiver
更改为IntentService
和PendingIntent.getBroadcast
更改为PendingIntent.getService
- >来解雇服务。结果是一样的。 onHandleIntent
收到的Intent
没有数据。而no data
我的意思是,例如intent.getStringExtra(AlarmService.CONTENT_STATUS)
返回null。我该如何解决这个问题?
编辑:
我正在考虑改变方法。也许我应该切换到JobScheduler
来摆脱这个错误?你对我的想法有什么想法?
编辑2:
原来我的Parcelable
对象序列化存在问题。我有Parcelable
个子对象,它们在序列化方面存在一些问题。不幸的是,Android O没有通知这一点。我发现从使用O
的设备切换到较旧的设备后出现了什么问题。
答案 0 :(得分:0)
解决了在清单中声明这种方式的广播接收器
<receiver android:name=".services.AlarmReceiver"
android:enabled="true"
android:exported="false"
>
<intent-filter>
<action android:name="com.blogspot.shudiptotrafder.lifeschedular"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
并且由于我的这种活动而打电话
val intent = Intent(context, AlarmReceiver::class.java)
intent.putExtra(ALARM_EXTRA, alarm)
intent.putExtra("1123","Hola mundo!")
val pIntent = PendingIntent.getBroadcast(
context,
112358,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
)
Log.e("1123 time seetteedd",nextAlarm.timeInMillis.toString())
val am = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
am.set(AlarmManager.RTC_WAKEUP, nextAlarm.timeInMillis, pIntent)