0

I am working on developing an Android app that will detect airplane mode throughout whole application. where to i add this code?

@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static boolean isAirplaneModeOn(Context context) {        
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
        return Settings.System.getInt(context.getContentResolver(), 
                Settings.System.AIRPLANE_MODE_ON, 0) != 0;          
    } else {
        return Settings.Global.getInt(context.getContentResolver(), 
                Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
    }       
}
Raj
  • 950
  • 1
  • 9
  • 33

2 Answers2

1

You can add this code where you want to detect whether connection is available.

for example:

@Override
    public void onClick(View v) {
boolean isonair = myApp.isAirplaneModeOn(this);
Toast.makeText(this, "IS on AIR? " + isonair, Toast.LENGTH_LONG).show();
}

If you want to have an access for static function use them in Application class:

public class myApp extends Application {
public static function isAirplaneModeOn() {
...
}
}

in any activity use this access: myApp.isAirplaneModeOn()

do not forget update your AndroidManifest.xml:

<application
        android:largeHeap="true"
        android:name=".myApp" <<<<<<<<<<<<<<<<<<< this is your class name
        android:icon="@drawable/somedrawable"
        android:label="@string/app_alias"
        android:theme="@style/AppTheme" >
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
0

Create a Interface like AirplaneModeManager whose Implementation extending broadcast receiver to get notified in the scenario when airplane mode is activated

you can use this code in Implementation to keep a track and use its methods in whole application to get the status !!

Punit Sharma
  • 2,951
  • 1
  • 20
  • 35