10

I have tried to set mobile data. But it just worked for only SIM 1 .

public static void setMobileData(Context context, boolean isEnabled) throws NoSuchFieldException, ClassNotFoundException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {

    ConnectivityManager conman = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    @SuppressWarnings("rawtypes")
    final Class conmanClass = Class.forName(conman.getClass().getName());
    final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
    iConnectivityManagerField.setAccessible(true);
    final Object iConnectivityManager = iConnectivityManagerField.get(conman);
    final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());

    Class[] cArg = new Class[2];
    cArg[0] = String.class;
    cArg[1] = Boolean.TYPE;
    Method setMobileDataEnabledMethod;

    setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", cArg);

    Object[] pArg = new Object[2];
    pArg[0] = context.getPackageName();
    pArg[1] = isEnabled;
    setMobileDataEnabledMethod.setAccessible(true);
    setMobileDataEnabledMethod.invoke(iConnectivityManager, pArg);
}

public static void setMobileData2(Context context, boolean isEnabled) throws NoSuchMethodException, ClassNotFoundException, IllegalAccessException, NoSuchFieldException, InvocationTargetException {
    final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    final Class conmanClass = Class.forName(conman.getClass().getName());
    final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
    iConnectivityManagerField.setAccessible(true);
    final Object iConnectivityManager = iConnectivityManagerField.get(conman);
    final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
    final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
    setMobileDataEnabledMethod.setAccessible(true);

    setMobileDataEnabledMethod.invoke(iConnectivityManager, isEnabled);
}

public static boolean setMobileData3(Context context, boolean isEnable) {
    boolean mobileDataAllowed = Settings.Secure.putInt(context.getContentResolver(), "mobile_data", isEnable?1:0);
    return mobileDataAllowed;
}

But now I just want to launch that default mobile selection Dialog . If you have Any idea to launch that dialog let me know.. thanks in advance.

.

shobhan
  • 1,460
  • 2
  • 14
  • 28

3 Answers3

3

You have to start intent for settings like this.

startActivityForResult(new Intent(android.provider.Settings.ACTION_DATA_ROAMING_SETTINGS), 0);

'Note' There are also settings of WIFI and more .Explore it the much you want. like this

android.provider.Settings.ACTION_WIFI_SETTINGS
Arslan Ashraf
  • 867
  • 7
  • 12
  • 2
    thank you for your response @Arslan. That intent is used to launch 'network settings'. but what I want is directly launch a popUp to select mobile data between SIM1,SIM2 or OFF. – shobhan Dec 10 '15 at 09:28
  • With this solution dual sim settings dialog will never come. – Daud Arfin Dec 16 '15 at 14:40
  • Network setting is different from dual sim dialog for data selection – siva Dec 16 '15 at 15:03
3

Multi SIM support is added in android lollipop 5.1 onwards only. Prior to that different phone manufacturers have their own custom implementation for supporting Multi SIM and respective settings. Hence if you are targeting for general solution it is not possible to achieve. Even on 5.1, there is no direct intent to launch this particular setting but using a hack you may achieve provided manufacturers should use only Google solution otherwise it will not work.

siva
  • 1,850
  • 13
  • 14
1

I have tried to open internet setting thriugh my application but it is by default functionality that it opens internet only the default sim i.e. Sim 1.You have to redirect user on to the setting screen using intent

Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS); startActivity(intent);