Android BroadCastReceiver and service with example
Two main components in android so every android developer has to be familiar with them .
Broad cast receiver
Who have not heard about broadcasting so let me tell you what its meaning
To broadcast information that means transmit the information to many receivers
For example radio station transmit signal to all radio receivers
The same thing in android we have some components that take broad cast receivers to notify them when specific action or event happen for example alarm manager takes a specific time and broadcast receiver to notify it every day at this specific time .
Alarm Manager simple code example with some explains
This snippet of below class
val i = Intent(context, YourBroadCastClass::class.java)
val pi = PendingIntent.getBroadcast(context, 0, i, 0)val am = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
//get instance of system alarm manageram.setRepeating( AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), (1000*60*60*24).toLong(),
pi // pending intent)
setRepeating method takes four parameters
type of alarm ,
the start time of alarm like I want to do something at 7am ,
the interval time (for ex after 7 am every 24 hour repeat the alarm)
and the broadcast class pending : every time this event happen open the broad cast class and fire on receive method , as we know the alarm manager is separated component from our application so you cannot perform broadcast action using normal intent instead use pending intent
By giving a Pending Intent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself (with the same permissions and identity)
class YourBroadCastClass : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { // here you can do your action like play sound or show notification } fun setAlarm(context: Context)Log.e("Alarm!!!!!!!","startAlarm") // For exampleval am = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
val i = Intent(context, AlertCreateOrder::class.java)
val pi = PendingIntent.getBroadcast(context, 0, i, 0)am.setRepeating( AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), (1000*60*60*24).toLong(), pi ) // Millisec * Second * Minute* Hours } fun cancelAlarm(context: Context) { Log.e("Alarm!!!!!!!","cancelAlarm") // For example val intent = Intent(context, YourBroadCastClass::class.java) val sender = PendingIntent.getBroadcast(context, 0, intent, 0) val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager alarmManager.cancel(sender) }
don’t forget to define broadcast in manifest class
<receiver android:process=":remote" android:name="your broad cast class"/>Service:
Android component that long-running operations in the background and the operations can still work when the application is closed
So ask yourself why when i close music app the song still work?!
Yes, the answer is this app use service .
Imagine example I mentioned it above about radio station transmit signal to radio receivers but your receiver is off, do you expect get signal! Of course you cannot
The same thing in android ,you have to guarantee the broadcast receiver still work even if user close the application
One of the solutions is to use service.
So all what you have to do is Create your Service class that inherit from Service , override service methods, get instance of broadcast inside service
And start service from your application activity.
class YourSerice : Service() { var alarm = BroadCastClass() override fun onUnbind(intent: Intent?): Boolean { return super.onUnbind(intent) } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { alarm.setAlarm(this) } override fun onStart(intent: Intent?, startId: Int) { alarm.setAlarm(this) } override fun onBind(intent: Intent?): IBinder? { return null } override fun onCreate() { super.onCreate() } override fun onDestroy() { Log.e("Alarm!!!!!!!","destroyserice") // For example alarm.cancelAlarm(this) super.onDestroy() }
And don’t forget to define your service in manifest class
<service android:name=".your servie class "/>The last step is to start service in your activity
startService(Intent(this, YourService::class.java))