我正在显示DatePicker以使用户将来选择日期,我想在所选日期的前一天通知用户。例如,如果用户选择日期[2020年4月26日],即使该应用未运行,我也要在[2020年4月25日]通知用户。但是,就我而言,该通知仅在应用程序运行时出现,但在应用程序未运行时通知不起作用。
这是我的MainActivity
class MainActivity : AppCompatActivity() {
private lateinit var dateFormat: SimpleDateFormat
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
dateFormat = SimpleDateFormat("MMM dd, yyyy", Locale.US)
edit_text_date.setOnClickListener {
showDatePicker()
}
notify_switch.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked) {
val deadlineDate = dateFormat.parse(edit_text_date.text.toString())!!
val c = Calendar.getInstance()
c.time = deadlineDate
c.add(Calendar.DATE, -1)
val pastDay = dateFormat.format(c.time)
val calender = Calendar.getInstance()
val currentDate = dateFormat.format(calender.time)
if (currentDate == pastDay) {
val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val intent = Intent(this, AlarmReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0)
alarmManager.set(
AlarmManager.RTC_WAKEUP,
c.timeInMillis,
pendingIntent
)
}
}
}
}
private fun showDatePicker() {
val c = Calendar.getInstance()
DatePickerDialog(this,DatePickerDialog.OnDateSetListener { view, year, month, dayOfMonth ->
val daySelected = Calendar.getInstance()
daySelected.apply {
set(Calendar.YEAR, year)
set(Calendar.MONTH, month)
set(Calendar.DAY_OF_MONTH, dayOfMonth)
}
edit_text_date.setText(dateFormat.format(daySelected.time))
},
c.get(Calendar.YEAR),
c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH)
).show()
}
}
这是AlarmReceiver类
class AlarmReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent?) {
showNotification(context,"title","notification message")
}
private fun showNotification(context: Context,title:String,body:String){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
val notificationChannel = NotificationChannel("CHANNEL_ID","My Channel",NotificationManager.IMPORTANCE_HIGH)
val notificationManager = context.getSystemService(NotificationManager::class.java)!!
notificationManager.createNotificationChannel(notificationChannel)
}
val notificationManagerCompat = NotificationManagerCompat.from(context)
val notification = NotificationCompat.Builder(context,"CHANNEL_ID")
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle(title)
.setContentText(body)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.build()
notificationManagerCompat.notify(0,notification)
}
}
我也在清单上注册了接收者
为了更有效地安排任务,您应该使用新引入的WorkManager类。它处理所有API级别,您不必担心应用程序可以使用的Android版本。
要在设定的时间调用函数,
OneTimeWorkRequest oneTimeWorkRequest = new OneTimeWorkRequest.Builder(WorkerClass.class)
.setConstraints(constraints)
.setInitialDelay(delayToPass, TimeUnit.MILLISECONDS)
.build();
WorkManager.getInstance(getApplicationContext())
.enqueueUniqueWork(oneTimeWorkRequest);
有关更多信息,请检出:https://developer.android.com/reference/androidx/work/WorkManager
“ WorkerClass”是将在[]中处理所需工作的类
public Result doWork() // method
请确保您也阅读过它。