-1

I'm trying to enable/disable data mobile in specific moments. I've tried it on MotoG 4.4.2 (Rooted) with it I had no problem to do this, but when I've tried it on Samsung Galaxy Grand Prime 4.4.4 (No Root) it doesn't work, also I've tried it on Samsung Galaxy S3 4.3 (No Root) but it worked.

  1. MotoG Worked -- WHY? -- ROOT Acces
  2. Samsung Galaxy Grand Prime didn't work -- WHY? -- I think it doesn't work because when I try to do it manually I've to accept a dialog that I want to lose data connectivity, and I think this is the problem because it doesn't work.
  3. Samsung Galaxy S3 worked -- WHY? -- I think it worked because the user put "Don't show me again" and user accepted it before.

The code that I'm trying is :

public void GetDataConnectionAPI() {
    this.context.getApplicationContext();
    TelephonyManager telephonyManager =
            (TelephonyManager) this.context.getApplicationContext().
                    getSystemService(Context.TELEPHONY_SERVICE);
    try {
        telephonyManagerClass = Class.forName(telephonyManager.getClass().getName());
        Method getITelephonyMethod = telephonyManagerClass.getDeclaredMethod("getITelephony");
        getITelephonyMethod.setAccessible(true);
        ITelephonyStub = getITelephonyMethod.invoke(telephonyManager);
        ITelephonyClass = Class.forName(ITelephonyStub.getClass().getName());

        dataConnSwitchmethod_OFF =
                ITelephonyClass.getDeclaredMethod("disableDataConnectivity");
        dataConnSwitchmethod_ON = ITelephonyClass.getDeclaredMethod("enableDataConnectivity");
    } catch (Exception e) { 
        e.printStackTrace();
    }
}

And then I enable/disable the data with this method :

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

    setMobileDataEnabledMethod.invoke(connectivityManager, enabled);

}

When I want to Enable I call this method :

public void EnableMobileData(){
    try {
        setMobileDataEnabled(context, true);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
}

On my Manifest I put

<!-- 3g uses-permisions-->
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />

I wonder if it's any possibility to avoid this dialog or just accept it programmatically..

I've read Bojan Kogoj answer that sais that I've to :

  • Pre-installed into a system folder on the ROM
  • Compiled by a manufacturer using their security certificate

But I'm wondering if is any other way to do this.

Community
  • 1
  • 1
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148

1 Answers1

3

The MODIFY_PHONE_STATE permission is not guaranteed to work in all cases. By default it is a system-level permission, so it will only work on devices whose manufacturers have enabled it.

Similarly, methods like getITelephony() are device/manufacturer-specific, and there is no way to get the name of the methods for doing so on every device.

Unfortunately there is no guaranteed way to do this.

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120