Blog purpose for android basic example for android app developer. any query please my contact

Thursday, 2 February 2023

kotlin broadcast receiver in android studio

 kotlin broadcast receiver in android studio


Here's an example of how you can create a Kotlin broadcast receiver in Android:


import android.content.BroadcastReceiver

import android.content.Context

import android.content.Intent


class MyBroadcastReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context?, intent: Intent?) {

        // Perform actions based on the broadcast

        if (intent?.action == Intent.ACTION_BOOT_COMPLETED) {

            // Perform action on boot completed

        }

    }

}





You will also need to register your broadcast receiver in the AndroidManifest.xml file:



<receiver android:name=".MyBroadcastReceiver">

    <intent-filter>

        <action android:name="android.intent.action.BOOT_COMPLETED"/>

    </intent-filter>

</receiver>



In this example, the broadcast receiver listens for the BOOT_COMPLETED action and performs a specific action when it's received. You can add more intent filters to listen for other broadcast events.

No comments:

Post a Comment