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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With