Creating a broadcast receiver

Create a new file and name it AlarmReceiver, and have it extend BroadcastReceiver. Then, update it with the following code:

class AlarmReceiver: BroadcastReceiver() {

override fun onReceive(context: Context?, p1: Intent?) {
Log.d("onReceive", "p1$p1")
val i = Intent(context, AlarmService::class.java)
context?.startService(i)
}
}

All you did was to override the onReceive method to start an IntentService called AlarmService (this class will be responsible for showing the notification). Well, the log statement is only there to help with debugging.

Before moving on, register the service in your AndroidManifest.xml just as the MainActivity component is. Here, you only need the name property, though:

<application>
...

<service android:name=".AlarmReceiver"/>
</application>

Now, proceed to create the AlarmService started by the AlarmReceiver.

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

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