0% found this document useful (0 votes)
24 views2 pages

Broadcast Receiver and Intent Filter

The document discusses registering a broadcast receiver to monitor changes to the airplane mode setting on Android. It defines an AirplaneBroadcastReceiver class that extends BroadcastReceiver and overrides the onReceive method. This broadcast receiver is registered in the MainActivity onCreate method to listen for ACTION_AIRPLANE_MODE_CHANGED intents using an IntentFilter. When an airplane mode change occurs, the broadcast receiver calls the airplaneProcess method on the activity to update the UI.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views2 pages

Broadcast Receiver and Intent Filter

The document discusses registering a broadcast receiver to monitor changes to the airplane mode setting on Android. It defines an AirplaneBroadcastReceiver class that extends BroadcastReceiver and overrides the onReceive method. This broadcast receiver is registered in the MainActivity onCreate method to listen for ACTION_AIRPLANE_MODE_CHANGED intents using an IntentFilter. When an airplane mode change occurs, the broadcast receiver calls the airplaneProcess method on the activity to update the UI.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Broadcast receiver and Intent

filter
public class MainActivity extends AppCompatActivity implements IAirplaneModeProcessable {

AirplaneBroadcastReceiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//check the first state of airplane when launching the activity


boolean isAirplane = Settings.System.getInt(this.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
this.airplaneProcess(isAirplane);

//register a broadcast receiver for updating the state of airplane mode


receiver = new AirplaneBroadcastReceiver(this);
IntentFilter intentFilter = new
IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
this.registerReceiver(receiver, intentFilter);

@Override
public void airplaneProcess(boolean isAirplaneMode) {
String text = "Airplane mode is off";
if (isAirplaneMode) {
text = "Airplane mode is on";
}
TextView status = findViewById(R.id.airplaneStatus);
status.setText(text);
}

@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
}

interface IAirplaneModeProcessable {
public void airplaneProcess(boolean isAirplaneMode);
}

Broadcast receiver and Intent filter 1


class AirplaneBroadcastReceiver extends BroadcastReceiver {
IAirplaneModeProcessable processor;

AirplaneBroadcastReceiver(IAirplaneModeProcessable processor) {
this.processor = processor;
}

@Override
public void onReceive(Context context, Intent intent) {
//only process the ACTION_AIRPLANE_MODE_CHANGED
if(intent.getAction().compareTo( Intent.ACTION_AIRPLANE_MODE_CHANGED)==0){
this.processor.airplaneProcess(intent.getBooleanExtra("state", false));
}
}
}

Broadcast receiver and Intent filter 2

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy