Creating the alarm

Basically, there are four types of alarms:

  • Elapsed Realtime: This fires the pending intent based on the amount of time since the device was booted, but doesn't wake up the device. The elapsed time includes any time during which the device was asleep.
  • Elapsed Realtime Wakeup: This wakes up the device and fires the pending intent after the specified length of time has elapsed since device boot.
  • RTC: This fires the pending intent at the specified time, but does not wake up the device.
  • RTC Wakeup: This wakes up the device to fire the pending intent at the specified time.

You are going to use RTC Wakeup alarm type to wake up the device to fire the alarm at the precise time the user sets.

First, create a dialog for the user to select the time the alarm should go off. Create a new class called TimePickerFragment. Then, update it with the code shown here:

import android.app.AlarmManager
import android.app.Dialog
import android.app.PendingIntent
import android.app.TimePickerDialog
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.DialogFragment
import android.text.format.DateFormat
import android.util.Log
import android.widget.TimePicker
import android.widget.Toast
import java.util.Calendar


class TimePickerFragment : DialogFragment(), TimePickerDialog.OnTimeSetListener {

override fun onCreateDialog(savedInstanceState: Bundle): Dialog {
val c = Calendar.getInstance()
val hour = c.get(Calendar.HOUR_OF_DAY)
val minute = c.get(Calendar.MINUTE)

return TimePickerDialog(activity, this, hour, minute,
DateFormat.is24HourFormat(activity))
}

override fun onTimeSet(view: TimePicker, hourOfDay: Int, minute: Int) {
Log.d("onTimeSet", "hourOfDay: $hourOfDay minute:$minute")

Toast.makeText(activity, "Reminder set successfully", Toast.LENGTH_LONG).show()

val intent = Intent(activity, AlarmReceiver::class.java)
intent.putExtra(ARG_TASK_DESCRIPTION, taskDescription)

val alarmIntent = PendingIntent.getBroadcast(activity, 0, intent, 0)
val alarmMgr = activity.getSystemService(Context.ALARM_SERVICE) as AlarmManager

val calendar = Calendar.getInstance()
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay)
calendar.set(Calendar.MINUTE, minute)

alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, alarmIntent)
}
}

companion object {
val ARG_TASK_DESCRIPTION = "task-description"

fun newInstance(taskDescription: String): TimePickerFragment {
val fragment = TimePickerFragment()
val args = Bundle()
args.putString(ARG_TASK_DESCRIPTION, taskDescription)
fragment.arguments = args
return fragment
}
}

In the onCreateDialog method, you created an instance of the  TimePickerDialog and set the default time to the current time. So, when the time picker starts, it will display the current time.

Then, you overrode the onTimeSet method to handle the user's set time. You first logged the time, then displayed a toast saying that the time had been set successfully and is recorded.

Then, you created an intent to execute the AlarmReceiver (you will create it very soon). Next was a PendingIntent, to be triggered when the alarm goes off. Then you (finally) created the alarm passing in the user's time. This alarm will be triggered at the exact time the user set. And, it will run only once.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset