Enable data connection

Status
Not open for further replies.

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   SetMobileDataEnabled(True)
End Sub

Sub SetMobileDataEnabled(enabled As Boolean)
   Dim r As Reflector
   r.Target = r.GetContext
   r.Target = r.RunMethod2("getSystemService", "connectivity", "java.lang.String")
   r.RunMethod2("setMobileDataEnabled", enabled, "java.lang.boolean")
End Sub

You should also add this line to the manifest editor:
B4X:
AddPermission(android.permission.CHANGE_NETWORK_STATE)

Note that this solution uses an hidden API.
 
Upvote 0

koezuma

Member
Licensed User
Longtime User
dear erel,

i try it and still get an error.

my steps are :
1. compile the script
2. edit and add AddPermission(android.permission.CHANGE_NETWORK_STATE)
to androidmanifest.xml
3. copy androidmanifest.xml and test.apk to device
4. install and run it

thx
koezuma
 

Attachments

  • error.zip
    22.4 KB · Views: 307
Upvote 0

koezuma

Member
Licensed User
Longtime User
great erel

i add : <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>

to AndroidManifest-template.xml on folder C:\Program Files\Anywhere Software\Basic4android.

problem solved.
:sign0098::sign0098:

koezuma
 
Upvote 0

PSEAD

Member
Licensed User
Longtime User
Hello Erel
I am using an Android "panel pc" that has a 3G modem usb dongle. I can toggle the "data enabled" setting from the Android's own settings screen to reset the modem when it disconnects, but cannot get it to toggle from code. (I have tried the toggle library and the code above -- no errors, but no effect). Using the getActiveNetworkInfo() - getTypeName, I can see it registers as a network type MODEM3G and not MOBILE, which is why I think the code does not have any effect. (I can successfully toggle tethering, airplane mode and wifi, but not the data enabled.) Do you know any of any other methods that I could try or could you give me some pointers?

Thanks
Paul
 
Last edited:
Upvote 0

PSEAD

Member
Licensed User
Longtime User
Ok, I address it to everybody then :)
I have seen that by switching Airplane mode on and off it has the same effect of redialing the data connection (using Android 4.1), but this will be a problem if I move to 4.2 as it seems they have blocked this capability going forward. I will keep looking for a long term solution.
 
Upvote 0

cyberlor

New Member
Licensed User
Longtime User
Here:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   SetMobileDataEnabled(True)
End Sub

Sub SetMobileDataEnabled(enabled As Boolean)
   Dim r As Reflector
   r.Target = r.GetContext
   r.Target = r.RunMethod2("getSystemService", "connectivity", "java.lang.String")
   r.RunMethod2("setMobileDataEnabled", enabled, "java.lang.boolean")
End Sub

You should also add this line to the manifest editor:
B4X:
AddPermission(android.permission.CHANGE_NETWORK_STATE)

Note that this solution uses an hidden API.

Woah :) Wonderful ! Thanks, Erel!
 
Last edited:
Upvote 0

Mark Hollingworth

Member
Licensed User
Longtime User
Not sure but I think this might be broken now (using Android 5.1.1)

I am getting the following error when trying to run this code.

java.lang.SecurityException: Neither user 10233 nor current process has android.permission.MODIFY_PHONE_STATE.
 
Upvote 0
Status
Not open for further replies.
Top