Android Broadcast Receivers
Broadcast messages from other apps or the system itself are responded to by broadcast receivers. Sometimes these signals are referred to as occurrences or intentions. Programs, for instance, may broadcast to inform other programs that certain data has been downloaded to the device and is ready for use. In this case, the broadcast receiver will intercept the broadcast and take the necessary action.
Static Broadcast Receivers: These receiver types are listed in the manifest file and continue to function even when the app is dismissed.
Dynamic Broadcast Receivers: Only the active or minimized version of the app is compatible with these receivers.
The two things that we have to do to use the broadcast receiver in our application are:
Creating the Broadcast Receiver:
Registering a Broadcast Receiver:
1. Create a new project and write the following code in the activity_main.xml file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> |
2. Write the following code. Below is the code for the MainActivity.java file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import android.content.IntentFilter; import android.os.Bundle; public class MainActivity extends AppCompatActivity { AirplaneModeChangeReceiver airplaneModeChangeReceiver = new AirplaneModeChangeReceiver(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override protected void onStart() { super.onStart(); IntentFilter filter = new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED); registerReceiver(airplaneModeChangeReceiver, filter); } @Override protected void onStop() { super.onStop(); unregisterReceiver(airplaneModeChangeReceiver); } } |
3. Create a java class name as AirplaneModeChangeReceiver.java and write the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.example.broadcastreciever; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.provider.Settings; import android.widget.Toast; public class AirplaneModeChangeReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (isAirplaneModeOn(context.getApplicationContext())) { Toast.makeText(context, "AirPlane mode is on", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(context, "AirPlane mode is off", Toast.LENGTH_SHORT).show(); } } private static boolean isAirplaneModeOn(Context context) { return Settings.System.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) != 0; } } |
The user will get output when the airplane mode will be on/off of the device.