Android Question Android 8 airplane mode and wifi

welu1805

Active Member
Licensed User
Longtime User
Hi all,

in Android 5, 6 and 7 if the airplane mode is active however it's possible to turn on wifi programatically. On my Android 8.1 tablet I can't turn on wifi.

Is this really so since Android 8?

Lutz
 

welu1805

Active Member
Licensed User
Longtime User
My question is: If airplane mode is active under Android 8 I can't turn on wifi. Is this correct?
 
Upvote 0

welu1805

Active Member
Licensed User
Longtime User
Here is an example to turn on or off wifi.

B4X:
#Region  Project Attributes
    #ApplicationLabel: TestWifiAirplane
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private Context As JavaObject
    Private Wifi As JavaObject
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private btnWifiOff As Button
    Private btnWifiOn As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")

  If FirstTime Then
        Context.InitializeContext
        Wifi = Context.RunMethod("getSystemService", Array("wifi"))
  End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub btnWifiOn_Click
    If Wifi.RunMethod("setWifiEnabled", Array(True)) Then
        Msgbox("Wifi is enabled", "Result")
    Else
        Msgbox("Wifi is Not enabled", "Result")
    End If
End Sub

Sub btnWifiOff_Click
    Wifi.RunMethod("setWifiEnabled", Array(False))
End Sub

In the manifest:

B4X:
AddPermission("android.permission.ACCESS_WIFI_STATE")
AddPermission("android.permission.CHANGE_WIFI_STATE")
AddPermission("android.permission.CHANGE_NETWORK_STATE")

I tested it on tablets with airplane mode on/off and under Android 5, 6, 7 and 8.1.

Result: In Android 5, 6, 7 it's possible to turn wifi on although airplane mode is on. Under Android 8.1 it's not possible to turn on wifi when airplane mode is on!
 
Upvote 0
Top