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;
}
}
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" >