3

我想从我的应用程序中禁用或启用在 android 设备中使用数据包,我在以下代码中使用了该数据包:

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());
    }

但是这段代码不能正常工作。我的朋友可以帮我解决这个问题吗?

4

1 回答 1

1

我认为您在 SO 答案中引用了此处的代码https://stackoverflow.com/a/4304110/582571

在上面的链接中评论提到“这在 Gingerbread 2.3+ 上不起作用”在另一个 SO 答案中查看详细说明https://stackoverflow.com/a/5095956/582571

所以最好不要制作任何带有隐藏 API 的应用程序

于 2012-06-28T14:03:38.150 回答