簡體   English   中英

啟用/禁用Android代碼中的使用數據包

[英]Enable/disable use data packet from android code

我想在我的應用程序中禁用或啟用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());
    }

但是這段代碼運行不正常。 我的朋友可以幫我解決這個問題嗎?

我想你在SO答案https://stackoverflow.com/a/4304110/582571中引用代碼

在上面提到的鏈接中提到“這不適用於Gingerbread 2.3+”請在另一個SO答案中查看詳細解釋https://stackoverflow.com/a/5095956/582571

所以最好不要使用隱藏的API制作任何應用程序

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM