Saturday, 15 June 2013

Turn ON and Turn OFF Airplane Mode in Android

ScreenShots:


activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="83dp"
        android:onClick="airplaneModeOn"
        android:textOff="Airplane Mode OFF"
        android:textOn="Airplane Mode ON" />

</RelativeLayout>

MainActivity.java:
package com.ram.airplanemodedemo;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.provider.Settings.Global;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

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

@SuppressLint("InlinedApi")
private static boolean isAirplaneModeOn(Context context) {

return Settings.System.getInt(context.getContentResolver(),
Global.AIRPLANE_MODE_ON, 0) != 0;

}

@SuppressLint("NewApi")
public void airplaneModeOn(View view) {
try {

android.provider.Settings.System.putInt(getContentResolver(),
Global.AIRPLANE_MODE_ON,
isAirplaneModeOn(getApplicationContext()) ? 0 : 1);

Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", !isAirplaneModeOn(getApplicationContext()));
sendBroadcast(intent);
} catch (Exception e) {
Toast.makeText(this, "Exception occured during Airplane Mode ON", Toast.LENGTH_LONG)
.show();
}
}
}

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ram.airplanemodedemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
     
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.ram.airplanemodedemo.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>