Mobile data not included

I asked this question, but received no answer.

I used the following code to enable 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 like:

 setMobileDataEnabled(getBaseContext(), true/false); 

This correctly enables / disables mobile data, but this code does not work correctly on devices with two SIM cards. I tested it on Motorola Razr D1, D3, in Samsung Dual-SIM (I don’t remember now), but this code does not work. Everything works fine, the application does not crash.

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

I found out that Android is not intended for Dual-Chip devices, this can be a problem, because I can’t target any SIM card, so I can’t find any trick or anything else to β€œfix” the code , I'm right?

What can I do to enable or disable mobile data on Dual-Chip devices? I took a look at the Source Code, setMobileDataEnabled is "publicly available", whe shouldn't have access to it?

I also find the IConnectivityManager class, but it is not a Java extension, I think it is .aidl or something (I do not remember), can this be useful?

I do not know what to do, please, I need help.

Sorry for my English.

Thanks.

+6
source share
1 answer

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

Using a universal method is always recommended. However, your code can only work with API 8 / API 10. Have you added permissions to the Android manifest file? Check the first answer to the related question and complete it.

If this does not work, check the second code identical to your code. But, I will recommend the first method, because in this way you do not have to write code for each API separately. In addition, as @tdevinda said, the code may differ from device versions from one device to one, from one sim-to-multi-sim, gsm-cdma-wcdma, API level and device manufacturers. Hope this helps :)

EDIT: There is no official help on the Android developers page yet. However, you can submit a request. I also could not find a solution. Dual-sim problem not resolved.

EDIT 2: Check the image. Solution trick

+1
source

Source: https://habr.com/ru/post/955949/


All Articles