Manoj Kumar Baghel
Manoj Kumar Baghel

Reputation: 96

Enable/disable use data packet from android code

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 ?

Upvotes: 3

Views: 2504

Answers (1)

rajpara
rajpara

Reputation: 5203

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

Upvotes: 1

Related Questions