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:
Unfortunately, Flightmodeapp has stopeed.
ReplyDeleteCan you help me?
(LG LEON, android 5.0.1, rooted
Thanks All Working fine
ReplyDeleteE/AndroidRuntime: FATAL EXCEPTION: main
ReplyDeleteProcess: com.example.turnonsettings, PID: 2675
java.lang.SecurityException: Permission denial: writing to settings requires:android.permission.WRITE_SECURE_SETTINGS
at android.os.Parcel.readException(Parcel.java:1683)
WHAT SHOULD I DO TO RESOLVE THIS ?
Really appreciate this wonderful post that you have provided for us.Great site and a great topic as well i really get amazed to read this. Its really good. charter flights
ReplyDeleteUcuz uçuşların tanıtılması da hava yolculuğunun popülaritesine katkıda bulunmuştur. Bilet fiyatları artık sıradan insanların ulaşabileceği bir mesafede. turna
ReplyDelete