Skip to content

Instantly share code, notes, and snippets.

@Seniru
Created June 12, 2025 13:47
Show Gist options
  • Save Seniru/af595853af4f9b2fc265dd88907bca61 to your computer and use it in GitHub Desktop.
Save Seniru/af595853af4f9b2fc265dd88907bca61 to your computer and use it in GitHub Desktop.
Broadcast receivers example
package com.seniru.broadcastreceiverexample
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import android.widget.Toast
class AirplaneModeBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Toast.makeText(context, "Airplane mode changed", Toast.LENGTH_LONG).show()
Log.i("AirplaneMode", intent?.data.toString())
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.BroadcastReceiverExample"
tools:targetApi="31">
<receiver android:name=".CustomEventBroadcastReceiver" android:exported="false">
<intent-filter>
<action android:name="com.seniru.TEST_ACTION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.seniru.broadcastreceiverexample
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.widget.Toast
class CustomEventBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Toast.makeText(context, "Button click event", Toast.LENGTH_SHORT).show()
}
}
package com.seniru.broadcastreceiverexample
import android.os.Bundle
import android.widget.Button
import androidx.core.content.ContextCompat
import androidx.appcompat.app.AppCompatActivity
import android.content.IntentFilter
import android.content.Intent
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button: Button = findViewById(R.id.button)
button.setOnClickListener {
// send broadcast action to the receiver
val intent = Intent()
intent.action = "com.seniru.TEST_ACTION"
sendBroadcast(intent)
}
// system message broadcast message receiver
val filter = IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED)
registerReceiver(AirplaneModeBroadcastReceiver(), filter)
// custom event broadcast message receiver
val customEventFilter = IntentFilter("com.seniru.TEST_ACTION")
ContextCompat.registerReceiver(this, CustomEventBroadcastReceiver(), customEventFilter, ContextCompat.RECEIVER_NOT_EXPORTED)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment