How can one detect airplane mode on Android

0 votes

I have code in my application that detects if Wi-Fi is actively connected. 

That code triggers a RuntimeException if airplane mode is enabled. 

I would like to display a separate error message when in mode is activated. How can I reliably detect if an Android device is in airplane mode?

May 26, 2020 in Java by kartik
• 37,520 points
1,375 views

1 answer to this question.

0 votes

Hello,

When registering the Airplane Mode BroadcastReceiver (@saxos answer) I think it makes a lot of sense to get the state of the Airplane Mode setting right away from the Intent Extras in order to avoid calling Settings.Global or Settings.System :

@Override
public void onReceive(Context context, Intent intent) {

    boolean isAirplaneModeOn = intent.getBooleanExtra("state", false);
    if(isAirplaneModeOn){

       // handle Airplane Mode on
    } else {
       // handle Airplane Mode off
    }
}

Hope this work!!

answered May 26, 2020 by Niroj
• 82,840 points

Related Questions In Java

0 votes
1 answer

How to pass an object from one activity to another on Android

One option could be letting your custom ...READ MORE

answered Jun 25, 2018 in Java by Rishabh
• 3,620 points
4,856 views
0 votes
1 answer

How can one day be added to a date?

One possible solution could be using calendar ...READ MORE

answered Jun 8, 2018 in Java by Daisy
• 8,140 points
628 views
0 votes
2 answers

How can I Copy files from one directory to another in Java

Java 8 Path sourcepath = Paths.get("C:\\data\\temp\\mydir"); ...READ MORE

answered Aug 10, 2018 in Java by samarth295
• 2,220 points
4,427 views
0 votes
1 answer

How can we run the .jar file by double clicking on windows 7 (64)?

Use the following method to run the ...READ MORE

answered Jun 28, 2018 in Java by sharth
• 3,370 points
1,854 views
+1 vote
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,620 points
1,330 views
+1 vote
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,290 points
1,188 views
+1 vote
1 answer

Performance difference of if/else vs switch statement in Java

The thing you are worried about is ...READ MORE

answered Jul 26, 2018 in Java by geek.erkami
• 2,680 points
3,748 views
0 votes
1 answer

How to pass an object from one activity to another on Android?

Hello @kartik, Implement your class with Serializable. Let's ...READ MORE

answered Apr 8, 2020 in Java by Niroj
• 82,840 points
772 views
0 votes
1 answer

How can we activate JMX on my JVM for access with jconsole?

Hello @kartik, Run your java application with the ...READ MORE

answered Apr 9, 2020 in Java by Niroj
• 82,840 points
1,295 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP