2012-06-28 19 views
3

我要禁用或從我的應用程序從我有如下代碼中使用啓用的Android設備使用數據包/禁止使用的數據包:啓用從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()); 
    } 

但是這個代碼不工作精細。我的朋友可以幫我解決這個問題嗎?

+0

當你說「它不工作」時,你怎麼知道的?發佈你的logcat。你得到什麼錯誤?告訴我們你在哪裏得到錯誤。 –

+0

另外,你用什麼設備來測試呢? –

+0

@大衛碼運行良好,但沒有舉行任何行動意味着我的使用數據包數據選項是不會改變..你可以有另一個想法,或者我能做些什麼改變我的代碼PLZ回覆謝謝 –

回答