Android Airplane Mode, Dark Mode, Hotspot on off BroadcastReceiver example
1/ Android Airplane Mode BroadcastReceiver
1.1 / Create A Class AirplaneModeReceiver class and extends BroadcastReceiver
package com.example.airplanemodebroadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class AirplaneModeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
boolean isAirplaneModeOn = intent.getBooleanExtra("state", false);
if (isAirplaneModeOn) {Toast.makeText(context, "Airplane Mode Enabled", Toast.LENGTH_SHORT).show();}
} else {
Toast.makeText(context, "Airplane Mode Disabled", Toast.LENGTH_SHORT).show();
}
}
1.2 / Active registerReceiver In MainActivity
package com.example.airplanemodebroadcastreceiver;
import androidx.appcompat.app.AppCompatActivity;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {BroadcastReceiver airplaneModeReceiver = new AirplaneModeReceiver();
IntentFilter intentFilter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intentFilter = new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
registerReceiver(airplaneModeReceiver, intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(airplaneModeReceiver);
}
}
1.3 / Then Change AndroidManifest.xml (Add receiver in the application )
<?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.AirplaneModeBroadcastReceiver"
tools:targetApi="31">
<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>
<receiver android:name=".AirplaneModeReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE" />
</intent-filter>
</receiver>
</application>
</manifest>
Now run the application and turn airplane mode on or off. Toast will show whether airplane mode is on or off.
2 / Bluetooth BroadcastReceiver
2.1 / Create A Class BluetoothReceiver class and extends BroadcastReceiver
package com.example.bluetoothonoffbroadcastreceiver;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class BluetoothReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
if(state == BluetoothAdapter.STATE_OFF){
Log.d("BluetoothReceiver", "Bluetooth is OFF");
Toast.makeText(context, "Bluetooth is OFF", Toast.LENGTH_SHORT).show();
}
else if (state == BluetoothAdapter.STATE_ON){
Log.d("BluetoothReceiver", "Bluetooth is ON");
Toast.makeText(context, "Bluetooth is ON", Toast.LENGTH_SHORT).show();
}
else if (state == BluetoothAdapter.STATE_TURNING_OFF){
Log.d("BluetoothReceiver", "Bluetooth is TURNING OFF");
Toast.makeText(context, "Bluetooth is TURNING OFF", Toast.LENGTH_SHORT).show();
}
else if (state == BluetoothAdapter.STATE_TURNING_ON){
Log.d("BluetoothReceiver", "Bluetooth is TURNING ON");
Toast.makeText(context, "Bluetooth is TURNING ON", Toast.LENGTH_SHORT).show();
}
else if (state == BluetoothAdapter.STATE_CONNECTED){
Log.d("BluetoothReceiver", "Bluetooth is CONNECTED");
Toast.makeText(context, "Bluetooth is CONNECTED", Toast.LENGTH_SHORT).show();
}
else if (state == BluetoothAdapter.STATE_CONNECTING){
Log.d("BluetoothReceiver", "Bluetooth is CONNECTING");
Toast.makeText(context, "Bluetooth is CONNECTING", Toast.LENGTH_SHORT).show();
}
else if (state == BluetoothAdapter.STATE_DISCONNECTED){
Log.d("BluetoothReceiver", "Bluetooth is DISCONNECTED");
Toast.makeText(context, "Bluetooth is DISCONNECTED", Toast.LENGTH_SHORT).show();
}
else if (state == BluetoothAdapter.STATE_DISCONNECTING){
Log.d("BluetoothReceiver", "Bluetooth is DISCONNECTING");
Toast.makeText(context, "Bluetooth is DISCONNECTING", Toast.LENGTH_SHORT).show();
}
}
}
}
2.2 / Active registerReceiver In MainActivity
package com.example.bluetoothonoffbroadcastreceiver;
import androidx.appcompat.app.AppCompatActivity;
import android.bluetooth.BluetoothAdapter;
import android.content.IntentFilter;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
BluetoothReceiver bluetoothReceiver = new BluetoothReceiver();
IntentFilter intentFilter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(bluetoothReceiver, intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(bluetoothReceiver);
}
}
2.3 / Then Change AndroidManifest.xml (Give Permissions and Add receiver in the application )
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<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.BluetoothOnOffBroadcastReceiver"
tools:targetApi="31">
<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>
<receiver android:name=".BluetoothReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
</intent-filter>
</receiver>
</application>
</manifest>
Now run the application and turn Bluetooth on or off. Toast will show whether Bluetooth is on, off, or in another state.