3

I want to disable or enable use data packet in android device from my application from which I have used on code as below :

try {
        Method dataConnSwitchmethod;
        Class telephonyManagerClass;
        Object ITelephonyStub;
        Class ITelephonyClass;

        TelephonyManager telephonyManager = (TelephonyManager) NetworkMonitorDemoAppActivity.this.getSystemService(Context.TELEPHONY_SERVICE);

        if (telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED) {
            isEnabled = true;
        } else {
            isEnabled = false;
        }

        telephonyManagerClass = Class.forName(telephonyManager.getClass().getName());
        Method getITelephonyMethod = telephonyManagerClass.getDeclaredMethod("getITelephony");
        getITelephonyMethod.setAccessible(true);
        ITelephonyStub = getITelephonyMethod.invoke(telephonyManager);
        ITelephonyClass = Class
                .forName(ITelephonyStub.getClass().getName());

        if (isEnabled) {
            dataConnSwitchmethod = ITelephonyClass.getDeclaredMethod("disableDataConnectivity");
        } else {
            dataConnSwitchmethod = ITelephonyClass.getDeclaredMethod("enableDataConnectivity");
        }
        dataConnSwitchmethod.setAccessible(true);
        dataConnSwitchmethod.invoke(ITelephonyStub);
    } catch (Exception e) {
        System.out.println("error is occured in uses data packet enable & disable :-"+e.getMessage());
    }

But this code is not working fine. Can any my friend help me for this issue ?

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • when you say "it isn't working", how do you know? Post your logcat. What errors are you getting? Show us where you are getting the errors. – David Wasser Jun 28 '12 at 13:57
  • Also, what device are you using to test this? – David Wasser Jun 28 '12 at 14:03
  • @David Code is run fine but not held any action means my use packet data option is not change.. can you have another idea or what can I do change in my code plz reply Thanks – Manoj Kumar Baghel Jun 29 '12 at 05:24
  • yup I am testing our application at Samsung Galaxy Fit device. – Manoj Kumar Baghel Jun 29 '12 at 05:24
  • Another issues, my device is not rooted. would be run this code at un rooted device. – Manoj Kumar Baghel Jun 29 '12 at 06:36
  • Have you got Android 2.2 or 2.3 on that device? – David Wasser Jun 29 '12 at 06:47
  • Should work, but this depends on the manufacturer's implementation of the Telephony stuff. Have you checked the logcat for any permission errors or other errors? It may give you some logs about why it isn't doing anything. – David Wasser Jun 29 '12 at 09:19
  • 1
    You could also do some debugging and once you've got the ITelephonyClass, you could call `getDeclaredMethods()` on it and then look and see what methods it has and what parameters they take. You could also have a look at `IConnectivityManager.setMobileDataEnabled()` as that might work if the `ITelephony` thing does not. You'll also need to use reflection to use `IConnectivityManager` – David Wasser Jun 29 '12 at 09:22

1 Answers1

1

I think you refer code here in SO answer https://stackoverflow.com/a/4304110/582571

Comment in above link mention "This does not work on Gingerbread 2.3+" check out the detail explanation here in another SO answer https://stackoverflow.com/a/5095956/582571

So its better not to make any app with hidden API

Community
  • 1
  • 1
rajpara
  • 5,203
  • 1
  • 29
  • 46