Android Tutorial Programmatically Setting AIRPLANE_MODE_ON and Off

You can do it with the help of the Reflection library.
This code toggles the airplane mode each time you click on the activity:
B4X:
Expand Collapse Copy
Sub Process_Globals

End Sub
Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)

End Sub

Sub SetAirplaneMode(On As Boolean)
    Dim p As Phone
    If on = GetAirplaneMode Then Return 'already in the correct state
    Dim r As Reflector
    Dim contentResolver As Object
    r.Target = r.GetContext
    contentResolver = r.RunMethod("getContentResolver")
    Dim state As Int
    If on Then state = 1 Else state = 0
    r.RunStaticMethod("android.provider.Settings$System", "putInt", _
        Array As Object(contentResolver, "airplane_mode_on", state), _
        Array As String("android.content.ContentResolver", "java.lang.String", "java.lang.int"))
    Dim i As Intent
    i.Initialize("android.intent.action.AIRPLANE_MODE", "")
    i.PutExtra("state", "" & On)
    p.SendBroadcastIntent(i)
End Sub

Sub GetAirplaneMode As Boolean
    Dim p As Phone
    Return p.GetSettings("airplane_mode_on") = 1
End Sub

Sub Activity_Click
    SetAirplaneMode(Not(GetAirplaneMode))
    Log(GetAirplaneMode)
End Sub
Sub Activity_Resume

End Sub
In order for it to work you should add the following declaration to the manifest file (just before the closing </manifest>):
B4X:
Expand Collapse Copy
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
See the PreferenceActivity tutorial for more information about modifying the manifest file: http://www.b4x.com/search?query=PreferenceActivity+Tutorial

This code requires the Reflection and Phone libraries.
 
Using the Reflection library. I've tested getting it but not changing it.
B4X:
Expand Collapse Copy
   'get
   Dim Obj1 As Reflector
   Dim state As Int
   Dim args(2) As Object
   Dim types(2) As String   
   Obj1.Target = Obj1.GetContext
   Obj1.Target = Obj1.RunMethod("getContentResolver")
   args(0) = Obj1.Target
   types(0) = "android.content.ContentResolver"
   args(1) = "airplane_mode_on" 
   types(1) = "java.lang.String"
   state = Obj1.RunStaticMethod("android.provider.Settings$System", "getInt", args, types)   
   Msgbox(state,"Airplane mode on")
   ' change
   Dim args(3) As Object
   Dim types(3) As String   
   Obj1.Target = Obj1.GetContext
   Obj1.Target = Obj1.RunMethod("getContentResolver")
   args(0) = Obj1.Target
   types(0) = "android.content.ContentResolver"
   args(1) = "airplane_mode_on" 
   types(1) = "java.lang.String"
   args(2) = 1 ' 0 or 1
   types(2) = "java.lang.int"
   state = Obj1.RunStaticMethod("android.provider.Settings$System", "putInt", args, types)
 
You can do it with the help of the Reflection library.
This code toggles the airplane mode each time you click on the activity:
B4X:
Expand Collapse Copy
Sub Process_Globals

End Sub
Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)

End Sub

Sub SetAirplaneMode(On As Boolean)
    Dim p As Phone
    If on = GetAirplaneMode Then Return 'already in the correct state
    Dim r As Reflector
    Dim contentResolver As Object
    r.Target = r.GetContext
    contentResolver = r.RunMethod("getContentResolver")
    Dim state As Int
    If on Then state = 1 Else state = 0
    r.RunStaticMethod("android.provider.Settings$System", "putInt", _
        Array As Object(contentResolver, "airplane_mode_on", state), _
        Array As String("android.content.ContentResolver", "java.lang.String", "java.lang.int"))
    Dim i As Intent
    i.Initialize("android.intent.action.AIRPLANE_MODE", "")
    i.PutExtra("state", "" & On)
    p.SendBroadcastIntent(i)
End Sub

Sub GetAirplaneMode As Boolean
    Dim p As Phone
    Return p.GetSettings("airplane_mode_on") = 1
End Sub

Sub Activity_Click
    SetAirplaneMode(Not(GetAirplaneMode))
    Log(GetAirplaneMode)
End Sub
Sub Activity_Resume

End Sub
In order for it to work you should add the following declaration to the manifest file (just before the closing </manifest>):
B4X:
Expand Collapse Copy
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
See the PreferenceActivity tutorial for more information about modifying the manifest file: Basic4android Search: PreferenceActivity Tutorial

This code requires the Reflection and Phone libraries.

Thanks a lot, but how can set it off?
 
B4X:
Expand Collapse Copy
SetAirplaneMode(false)

So if I want the two thing subsequentally, must do:

Sub Activity_Create(FirstTime As Boolean)
SetAirplaneMode()
SetAirplaneMode(false)
End Sub
 
If you want to turn airplane mode on the you should call:
B4X:
Expand Collapse Copy
SetAirplaneMode(True)
If you want to turn it off then you should call:
B4X:
Expand Collapse Copy
SetAirplaneMode(False)
Why would you want to turn it on and then immediately off?

Is useful to pacth a bug on a firmware for LG P500. :((

So I want make an app, then loaded put on and off the airplane mode.
 
Um. Why would you want to turn the airplane mode to off and on, while you could just switch it to on?

Just ON, wait a bit for example 250ms, and put it off..

Another question:
I'm tryiing to set
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>

but how I can set it? I change the xml, but it will be overwrited..
 
Hi Erel,

This code works
B4X:
Expand Collapse Copy
Sub Activity_Resume

   'Einstellung für Flugmodus
   SetAirplaneMode(True)

End Sub

they do not, why?
B4X:
Expand Collapse Copy
Sub Activity_Pause (UserClosed As Boolean)
   If UserClosed Then
      'Einstellung für Flugmodus
      SetAirplaneMode(False)
   End If
End Sub

Flight mode is ON.
Tried with HTC Android-One-S 4.03

Ciao,
Filippo
 
Have you added this line to the manifest editor:
Code:

AddPermission(android.permission.WRITE_SETTINGS)
Yes.

My Sub "Activity_Resume" looks like:
B4X:
Expand Collapse Copy
Sub Activity_Resume
   'Einstellung für Flugmodus
   Log("GetAirplaneMode=" & GetAirplaneMode & " ; cbxAirplane=" & manager.GetBoolean("cbxAirplane"))
   If manager.GetBoolean("cbxAirplane") <> GetAirplaneMode Then
      SetAirplaneMode(manager.GetBoolean("cbxAirplane"))
   End If
End Sub
 
Hi Erel,

This code works perfectly on my HTC Desire and an emulator (v4.0.3).
Only on my HTC-one-s not working.:(

B4X:
Expand Collapse Copy
Sub Activity_Resume
    'Einstellung f�r Flugmodus
    Log("GetAirplaneMode=" & GetAirplaneMode & " ; cbxAirplane=" & manager.GetBoolean("cbxAirplane"))
    If manager.GetBoolean("cbxAirplane") <> GetAirplaneMode Then
        SetAirplaneMode(manager.GetBoolean("cbxAirplane"))
    End If
End Sub
 
Top