Tuesday, 17 September 2013

On Launch

Control Airplane Mode

AirPlane Mode ON

Control Airplane Mode

AirPlane Mode OFF

Control Airplane Mode

Create new Android Project
Project Name: System Settings
//tested from 2.3.3 to 4.1 
Build Target: Android 2.3.3   //till 4.1
Application Name: SystemSettings
Package Name: com.shaikhhamadali.blogspot.systemsettings

Create layout file: activity_airplane_mode

1.create layout:

<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"
    tools:context=".AirplaneMode" >

    <TextView
        android:id="@+id/TVAirplaneMode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40sp"
        android:text="Air plane Mode is OFF:" />

    <ToggleButton
        android:id="@+id/tBAirplane"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="49dp"
        android:layout_toRightOf="@+id/TVAirplaneMode"
        android:textOn="Turn OFF"
        android:textOff="Turn ON" />

</RelativeLayout>
  • Add Permission in Manifest:

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>

2. code of activity:
package com.shaikhhamadali.blogspot.systemsettings;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.ToggleButton;

public class AirplaneMode extends Activity {
 // constants
 static final String STATUS_ON = "Airplane Mode: ON";
 static final String STATUS_OFF = "Airplane Mode: OFF";

 static final String TURN_ON = "Turn ON";
 static final String TURN_OFF = "Turn OFF";

 // controls
 TextView TVAirplaneMode;
 ToggleButton tBAirplane;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_airplane_mode);
  // load controls
  TVAirplaneMode = (TextView)findViewById(R.id.TVAirplaneMode);
  tBAirplane = (ToggleButton)findViewById(R.id.tBAirplane);
  // update UI at first time loading
  updateUI(isAirplaneMode());
  // set click event for button
  tBAirplane.setOnClickListener(new OnClickListener() {                     
   @Override
   public void onClick(View v) {
    // check current state first
    boolean state = isAirplaneMode();
    // toggle the state
    if(state)toggleAirplaneMode(0,state);
    else toggleAirplaneMode(1,state);
    // update UI to new state
    updateUI(!state);                               
   }
  });
 }

 public void toggleAirplaneMode(int value, boolean state) {
  // toggle airplane mode
  //check the version
  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { //if less than verson 4.2
   Settings.System.putInt(
     getContentResolver(),
     Settings.System.AIRPLANE_MODE_ON, value);
  } else {
   Settings.Global.putInt(
     getContentResolver(),
     Settings.Global.AIRPLANE_MODE_ON, value);
  }
  // broadcast an intent to inform
  Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
  intent.putExtra("state", !state);
  sendBroadcast(intent);
 }

 public void updateUI(boolean state) {
  //set text according to state
  if(state) {
   TVAirplaneMode.setText(STATUS_ON);
   tBAirplane.setText(TURN_OFF);             
  } else {
   TVAirplaneMode.setText(STATUS_OFF);
   tBAirplane.setText(TURN_ON);
  }
 }

 public boolean isAirplaneMode() {
  //check the version     
  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {//if less than verson 4.2
   return Settings.System.getInt(getContentResolver(), 
     Settings.System.AIRPLANE_MODE_ON, 0) != 0;          
  } else {
   return Settings.Global.getInt(getContentResolver(), 
     Settings.Global.AIRPLANE_MODE_ON, 0) != 0;

  }
 }
}

3. note that:
  • you can use this on button onclick,on action_down,on the fly etc.

4. conclusion:

  • Some information about how to toggle AirPlane Mode.
  • Know how to update UI after toggle.
  • know how to use different approaches according to device versions.

5. About the post:

  • The code seems to explain itself due to comments, if you have any question you can ask too!
  •  Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
  •  Hope you enjoy it!

6. Source Code:
        you can download the source code here

Cheers,

Hamad Ali Shaikh