我尝试在一个准确的时间发送通知,因此我添加了一个AlarmManager。触发AlarmReceiver,intent动作是设置好的,但是没有额外的。
我为AlarmManager做了一个简单的分离的AlarmService类AlarmService.kt:
private const val TAG = "AlarmService"
class AlarmService @Inject constructor(
@ApplicationContext private val context: Context
) {
private val alarmManager: AlarmManager? = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
private fun getPendingIntent(intent: Intent): PendingIntent =
PendingIntent.getBroadcast(
context,
0,//RandomUtil.getRandomInt(),
intent,
PendingIntent.FLAG_UPDATE_CURRENT
)
fun reminderAlarm(timeInMillis: Long, item: CustomParcelable){
val intent = Intent(context, AlarmReceiver::class.java)
intent.apply {
action = Constants.ACTION_SET_EXACT_ALARM_REMINDER
putExtra(Constants.EXTRA_EXACT_ALARM_TIME, timeInMillis)
putExtra(Constants.EXTRA_EXACT_ALARM_ITEM, CustomParcelable)
}
setAlarm(timeInMillis, getPendingIntent(intent))
}
private fun setAlarm(timeInMillis: Long, pendingIntent: PendingIntent){
alarmManager?.let {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
alarmManager.setExactAndAllowWhileIdle(
AlarmManager.RTC_WAKEUP,
timeInMillis,
pendingIntent
)
} else {
alarmManager.setExact(
AlarmManager.RTC_WAKEUP,
timeInMillis,
pendingIntent
)
}
}
}
一切都很好,但是当AlarmReceiver被触发时,只有intent ACTION,并且没有额外的数据,甚至时间数据,这是一个LONGAlarmReceiver.kt:
private const val TAG = "AlarmReceiver"
class AlarmReceiver: BroadcastReceiver() {
private lateinit var myNotification: MyNotification
override fun onReceive(context: Context, intent: Intent) {
Log.e(TAG, "onReceive: INTENT ------>>>>>>>---------->>>>>>>>>------->>>>>>>>>---------->>>>>>: "
+ intent.hasExtra(Constants.EXTRA_EXACT_ALARM_TIME) )
when(intent.action){
Constants.ACTION_SET_EXACT_ALARM_REMINDER -> {
val time: Long = intent.getLongExtra(Constants.EXTRA_EXACT_ALARM_TIME, 0L)
val item: CustomParcelable? = intent.getParcelableExtra(Constants.EXTRA_EXACT_ALARM_ITEM)
Log.e(TAG, "onReceive: ------------@@@@@@@@@@@@*-------------@@@@@@@@@------------"
+ item?.title + " reminder: " + item?.reminder + " t: " + time)
if (item != null) {
myNotification = MyNotification(context)
myNotification.createReminderNotificationChannel(item)
myNotification.sendNotificationReminder(item)
}
}
}
}
}
我想在intent中传递的项是Parcelable:
单品:
@Parcelize
data class CustomParcelable(
val title: String,
val reminder: String
) : Parcelable {
}
清单文件我使用所需的权限,并希望在手机重启后使警报可用。AndroidManifest:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".receiver.AlarmReceiver"
android:exported="true"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
我找到了解决方案,如何这些工作。只需将Parcelable项添加到bundle中:
class AlarmService @Inject constructor(
@ApplicationContext private val context: Context
) {
...
fun reminderAlarm(timeInMillis: Long, item: CustomParcelable){
val intent = Intent(context, AlarmReceiver::class.java)
intent.apply {
action = Constants.ACTION_SET_EXACT_ALARM_REMINDER
}
val bundle = Bundle()
bundle.putParcelable(Constants.EXTRA_EXACT_ALARM_ITEM, item)
intent.putExtra(Constants.EXTRA_EXACT_ALARM_DATA, bundle)
setAlarm(timeInMillis, getPendingIntent(intent))
}
}
现在在接收端,你可以访问intent:
class AlarmReceiver: BroadcastReceiver() {
private lateinit var myNotification: MyNotification
override fun onReceive(context: Context, intent: Intent) {
val data: Bundle? = intent.getBundleExtra(Constants.EXTRA_EXACT_ALARM_DATA)
when(intent.action){
Constants.ACTION_SET_EXACT_ALARM_REMINDER -> {
val data: Bundle? = intent.getParcelableExtra(Constants.EXTRA_EXACT_ALARM_DATA)
val item: CustomParcelable? = null
if (data != null){
item = data.getParcelable(Constants.EXTRA_EXACT_ALARM_ITEM)
if (item != null) {
myNotification = MyNotification(context)
myNotification.createReminderNotificationChannel(item)
myNotification.sendNotificationReminder(item)
}
}
}
}
}
}