Showing posts with label Flightmode or Airplane mode. Show all posts
Sunday, 31 May 2015
Flight mode or Airplane mode android example
Posted by
devraj chavda,
on
00:26
Hello friends,Let understand what is Airplane mode ?
It will cut of all signal transmissions from mobile called flights mode.In such cases people can put their phone on Flight mode instead of switching it off mobile and continue using other features.
Airplane mode is an setting in any mobile that suspends all the signal transmitting functions like calls, messaging,DATA connection, Bluetooth, WI-FI etc.in this tutorial is to show you how to switch Airplane mode ON/OFF. In Android Airplane Mode is something like a toggle button, it should be set to 1 - ON, 0 - OFF.
Step 1: Write code into 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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ImageButton
android:id="@+id/tBAirplane"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="104dp"
android:src="@drawable/flightmodeoff" />
</RelativeLayout>
Step 2: Write code into MainActivity.java
package dev.Androidapplink.flightmodeapp;
import dev.Androidapplink.flightmodeapp.R;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class MainActivity extends Activity {
// controls
ImageButton tBAirplane;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tBAirplane = (ImageButton) 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);
}
});
}
// Airplane mode version code
@SuppressLint("NewApi")
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
// then
// version
// 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 image according to state
if (state) {
tBAirplane.setImageResource(R.drawable.flightmodeon);
} else {
tBAirplane.setImageResource(R.drawable.flightmodeoff);
}
}
@SuppressLint("NewApi")
public boolean isAirplaneMode() {
// check the version
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {// if
// less
// than
// version
// 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;
}
}
}
NOTE: Give permission in AndroidManifest.xml
Its allow to change setting of device.
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
Step 3: Now Run Project:
Subscribe to:
Posts
(
Atom
)
check current state
(8)
android basic
(7)
Layout background
(6)
Network & Connectivity
(4)
Toast
(4)
XML
(4)
listview
(4)
Animation XML
(3)
Menu
(3)
web browser
(3)
Alert box
(2)
Calculator
(2)
Mobile data connection
(2)
Spinner
(2)
ToggleButton
(2)
Transparetn background
(2)
hello world
(2)
seekbar
(2)
set color
(2)
webview
(2)
wifi manager
(2)
Android interview Question
(1)
App uninstall
(1)
Battery
(1)
Bluetooth
(1)
CMD
(1)
CheckBox
(1)
Drag and Drop
(1)
Emi Loan
(1)
Flightmode or Airplane mode
(1)
Full screen
(1)
Maths
(1)
PopUp
(1)
SDcard
(1)
Sensor
(1)
Slider
(1)
Sound
(1)
Splash screen
(1)
Torch flash light
(1)
Transparent color
(1)
android architecture & fundamental
(1)
android folder and package
(1)
call
(1)
contact
(1)
converter
(1)
dialer
(1)
internet
(1)
screen brightness
(1)
sms
(1)
storage info
(1)
time
(1)
unite converter
(1)
| Follow me | Share |
|---|---|
| Follow @devraj205027 | Tweet |

