Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mobile Data is not turning on

Tags:

java

android

I asked this question but I received no answer.

I used the following code to turn on the mobile data (3G).

private static void setMobileDataEnabled(Context context, boolean enabled){

            try{
                ConnectivityManager conman = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
                Method setMobileDataEnabledMethod = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
                setMobileDataEnabledMethod.setAccessible(true);
                setMobileDataEnabledMethod.invoke(conman, enabled);

            }catch(NoSuchMethodException e){e.printStackTrace();}
            catch(InvocationTargetException e){e.printStackTrace();}
            catch(IllegalAccessException e){e.printStackTrace();}

        }

I call it as:

setMobileDataEnabled(getBaseContext(), true/false);

It enable/disable the mobile data correctly, but this code doesn't work correctly on Dual SIM devices. I tested it on Motorola Razr D1, D3, in a Samsung Dual-SIM (can't remember now) but this code does not work. Everything works fine, the application doesn't crash.

Instead of "getBaseContext()", I tried "getApplicationContext()" and "this", but nothing changed.

I learned that Android wasn't designed for Dual-Chip devices, this can be a problem since I can't target any SIM card, so I'm not able to find any trick or anything else to "fix" the code, am I right ?

What I can do to switch the mobile data on/off on Dual-Chip devices? I took a look on the Source codes, the setMobileDataEnabled is "public", whe shouldn't have access to it?

I also find IConnectivityManager class, but it is not a java extention, I think it is .aidl or something (can't remember), it can be usefull?

I don't know what to do, please I do need help.

Sorry for my English.

Thanks.

like image 399
Murillo Ferreira Avatar asked Oct 15 '13 01:10

Murillo Ferreira


1 Answers

try this link Enable/Disable Mobile Data (GPRS) using code

Using universal method is always recomended. HOwever, your code might only work with API 8/API 10. Did you added Permissions in the android manifest file? Check the first answer on the linked question and perform it.

If it doesn't work, check the second one which is identical to your code. But, i will recommend first method because in this way, you wouldn't have to write code for each API separately. Also as @tdevinda said, Code may vary from device-to-device, single-sim-to-multi-sim, gsm-cdma-wcdma versions, API levels and device-manufacturers. I hope it helps :)

EDIT: There is no official help from android developers page yet. HOwever, you can file a request. I was also unable to find any solution to this. Dual-sim problem is not solved.

EDIT 2: Check the image.Solution Trick

like image 146
rupinderjeet Avatar answered Oct 23 '22 07:10

rupinderjeet