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

Wednesday, 10 February 2021

kotlin broadcastreceiver example

 kotlin broadcastreceiver example


What is BroadcastReceiver in Android with example?

Android BroadcastReceiver is a dormant component of android that listens to system-wide broadcast events or intents. When any of these events occur it brings the application into action by either creating a status bar notification or performing a task. follow the google developer page broadcastreceiver.


follow the function broadcastreceiver:


override fun onStart() {
      super.onStart()
      val intentFilter = IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION)
      registerReceiver(wifiStateReceiver, intentFilter)
   }
   override fun onStop() {
      super.onStop()
      unregisterReceiver(wifiStateReceiver)
   }

follow the full example kotlin broadcastreceiver example

1.Brodcast.kt:

package com.akash.kotlin.broadcastreceiver 

import android.content.BroadcastReceiver

import android.content.Context

import android.content.Intent

import android.content.IntentFilter

import android.net.wifi.WifiManager

import android.os.Bundle

import android.widget.Switch

import android.widget.Toast

import androidx.appcompat.app.AppCompatActivity

class Brodcast : AppCompatActivity() {

    lateinit var wifiSwitch: Switch

    lateinit var wifiManager: WifiManager

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_brodcast)

        title = "KotlinApp"

        wifiSwitch = findViewById(R.id.wifiSwitch)

        wifiManager = applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager

        wifiSwitch.setOnCheckedChangeListener { _, isChecked ->

            if (isChecked) {

                wifiManager.isWifiEnabled = true

                wifiSwitch.text = "WiFi is ON"

            } else {

                wifiManager.isWifiEnabled = false

                wifiSwitch.text = "WiFi is OFF"

            }

        }

    }

    override fun onStart() {

        super.onStart()

        val intentFilter = IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION)

        registerReceiver(wifiStateReceiver, intentFilter)

    }

    override fun onStop() {

        super.onStop()

        unregisterReceiver(wifiStateReceiver)

    }

    private val wifiStateReceiver: BroadcastReceiver = object : BroadcastReceiver() {

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

            when (intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,

                WifiManager.WIFI_STATE_UNKNOWN)) {

                WifiManager.WIFI_STATE_ENABLED -> {

                    wifiSwitch.isChecked = true

                    wifiSwitch.text = "WiFi is ON"

                    Toast.makeText(this@Brodcast, "Wifi is On", Toast.LENGTH_SHORT).show()

                }

                WifiManager.WIFI_STATE_DISABLED -> {

                    wifiSwitch.isChecked = false

                    wifiSwitch.text = "WiFi is OFF"

                    Toast.makeText(this@Brodcast, "Wifi is Off", Toast.LENGTH_SHORT).show()

                }

            }

        }

    }

}



2.activity_brodcast.xml:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Brodcast ">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:padding="8dp"
        android:text="BrodCast "
        android:textColor="@color/colorPrimaryDark"
        android:textSize="48sp"
        android:textStyle="bold" />
    <Switch
        android:id="@+id/wifiSwitch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
</RelativeLayout>


3.AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.akash.kotlin.broadcastreceiver ">
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Brodcast">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

4.strings.xml:

<resources>
    <string name="app_name">kotlin broadcastreceiver </string>
</resources>


5.colors.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#6200EE</color>
    <color name="colorPrimaryDark">#3700B3</color>
    <color name="colorAccent">#03DAC5</color>
</resources>

6.styles.xml:

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

7.output:


kotlin broadcastreceiver example


broadcast receiver in android oreo example







1 comment: