我有通知服务和BroadcastReciver,可以通过计时器通知用户。但是我需要检查领域中的一些变量,如果这些变量是正确的开始通知给用户
不起作用-Service and BroadcastReciver中的Init领域 无效-executeTransactionAsync()和executeTransaction()
class NotificationService: IntentService("MyNewIntentService") {
override fun onHandleIntent(intent: Intent?) {
val id = intent!!.extras!!.getString("notifId")
var countCups = 0
realm.executeTransactionAsync {
val water = it.where(User::class.java).findFirst()!!.waterBalance
water.forEach {
if (it) {
countCups = countCups.plus(1)
}
}
}
when (id) {
"101" -> {
if (countCups < 3) {
NotificationHelper(applicationContext, id).creatNotivication(
"Пора выпить воды",
"Не забудьте отметить выпитый стаканчик в приложении"
)
}
}
"102" -> {
if (countCups < 5) {
NotificationHelper(applicationContext, id).creatNotivication(
"Пора выпить воды",
"Не забудьте отметить выпитый стаканчик в приложении"
)
}
}
"103" -> {
if (countCups < 8) {
NotificationHelper(applicationContext, id).creatNotivication(
"Пора выпить воды",
"Не забудьте отметить выпитый стаканчик в приложении"
)
}
}
}
}
}
class NotificationReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val id = intent!!.extras!!.getString("notifId")
val intent1 = Intent(context, NotificationService::class.java)
intent1.putExtra("notifId", id)
context.startService(intent1)
}
}
MainActivity中的方法
fun startNotificationAlarm(ctx: Context) {
val notificationIntent = Intent(ctx, NotificationReceiver::class.java)
notificationIntent.putExtra("notifId", "101")
val notificationIntent2 = Intent(ctx, NotificationReceiver::class.java)
notificationIntent2.putExtra("notifId", "102")
val notificationIntent3 = Intent(ctx, NotificationReceiver::class.java)
notificationIntent3.putExtra("notifId", "103")
val pendingIntent = PendingIntent.getBroadcast(ctx, 101, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT)
val pendingIntent2 = PendingIntent.getBroadcast(ctx, 102, notificationIntent2, PendingIntent.FLAG_UPDATE_CURRENT)
val pendingIntent3 = PendingIntent.getBroadcast(ctx, 103, notificationIntent3, PendingIntent.FLAG_UPDATE_CURRENT)
//12
val calendar = Calendar.getInstance()
calendar.set(Calendar.HOUR_OF_DAY, 12)
calendar.set(Calendar.MINUTE, 0)
calendar.set(Calendar.SECOND, 0)
//17
val calendar2 = Calendar.getInstance()
calendar2.set(Calendar.HOUR_OF_DAY, 17)
calendar2.set(Calendar.MINUTE, 0)
calendar2.set(Calendar.SECOND, 0)
//21
val calendar3 = Calendar.getInstance()
calendar3.set(Calendar.HOUR_OF_DAY, 21)
calendar3.set(Calendar.MINUTE,0)
calendar3.set(Calendar.SECOND, 0)
Log.v("Notification", "12:00 notification start")
var alarmManager = ctx.getSystemService(Context.ALARM_SERVICE) as AlarmManager
alarmManager.setRepeating(
AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 20000,
1 * 60 * 1000,
pendingIntent
)
Log.v("Notification", "17:00 notification start")
var alarmManager2 = ctx.getSystemService(Context.ALARM_SERVICE) as AlarmManager
alarmManager2.setRepeating(
AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + 20000,
1 * 60 * 1000,
pendingIntent2
)
Log.v("Notification", "21:00 notification start")
var alarmManager3 = ctx.getSystemService(Context.ALARM_SERVICE) as AlarmManager
alarmManager3.setRepeating(
AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + 20000,
1 * 60 * 1000,
pendingIntent3
)
}