2013-08-01 22 views
0

我想通過代碼啓用我的3G數據連接,無需任何用戶交互。一切都必須在背景中完成。在Android 4.0.4平板電腦後臺啓用數據連接的代碼

我嘗試了我在Google中找到的所有代碼,但沒有在我的Android平板電腦4.0.4中使用。 我插入3g SIM卡,重新啓動設備。但代碼不會自動調用Tablet.Data中的DataConnection。我的Tablet內部存儲器名稱將是「SDCard2」,因爲這個原因,該代碼存在任何問題。 請給我適當的解決方案。 我用下面的代碼:

public static void EnableInternet(Context mycontext) 
     { 
      try { 
       Log.i("Reached Enable", "I am here"); 
       Process proc; 
       try { 
        proc = Runtime.getRuntime().exec("su"); 
        try { 
         proc.waitFor(); 
        } catch (InterruptedException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       }   
       setMobileDataEnabled(mycontext,true); 
      } catch (NoSuchFieldException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (ClassNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IllegalArgumentException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IllegalAccessException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (NoSuchMethodException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (InvocationTargetException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

    void turnData(boolean ON) 
     { 
ConnectivityManager iMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 
      Method iMthd = null; 
      try { 
       iMthd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class); 
       } catch (Exception e) { 
         } 
      iMthd.setAccessible(false); 

      if(ON) 
      { 

         try { 
          iMthd.invoke(iMgr, true); 
          Toast.makeText(getApplicationContext(), "Data connection Enabled", Toast.LENGTH_SHORT).show(); 
         } catch (IllegalArgumentException e) { 
          // TODO Auto-generated catch block 
          // dataButton.setChecked(false); 
          Toast.makeText(getApplicationContext(), "IllegalArgumentException", Toast.LENGTH_SHORT).show(); 

         } catch (IllegalAccessException e) { 
          // TODO Auto-generated catch block 
          Toast.makeText(getApplicationContext(), "IllegalAccessException", Toast.LENGTH_SHORT).show(); 

          e.printStackTrace(); 
         } catch (InvocationTargetException e) { 
          // TODO Auto-generated catch block 
          // dataButton.setChecked(false); 
          Toast.makeText(getApplicationContext(), "InvocationTargetException", Toast.LENGTH_SHORT).show(); 

         } 

      } 
      else 
      { 
       try { 
        iMthd.invoke(iMgr, true); 
        Toast.makeText(getApplicationContext(), "Data connection Disabled", Toast.LENGTH_SHORT).show(); 
        } catch (Exception e) { 
          // dataButton.setChecked(true); 
         Toast.makeText(getApplicationContext(), "Error Disabling Data connection", Toast.LENGTH_SHORT).show(); 
              } 
      }} 





    boolean switchState(boolean enable) 
    { 
     boolean bRes = false; 

     // Data Connection mode (only if correctly initialized) 
     if (m_telManager != null) 
     { 
      try 
      { 
       // Will be used to invoke hidden methods with reflection 
       Class cTelMan = null; 
       Method getITelephony = null; 
       Object oTelephony = null; 
       Class cTelephony = null; 
       Method action = null; 

       // Get the current object implementing ITelephony interface 
       cTelMan = m_telManager.getClass(); 
       getITelephony = cTelMan.getDeclaredMethod("getITelephony"); 
       getITelephony.setAccessible(true); 
       oTelephony = getITelephony.invoke(m_telManager); 

       // Call the enableDataConnectivity/disableDataConnectivity method 
       // of Telephony object 
       cTelephony = oTelephony.getClass(); 
       if (enable) 
       { 
        action = cTelephony.getMethod("enableDataConnectivity"); 
       } 
       else 
       { 
        action = cTelephony.getMethod("disableDataConnectivity"); 
       } 
       action.setAccessible(true); 
       bRes = (Boolean)action.invoke(oTelephony); 
      } 
      catch (Exception e) 
      { 
       bRes = false; 
      } 
     }   
     return bRes; 
    }  





    try { 
    ConnectivityManager mgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 
       Method dataMtd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class); 
       dataMtd.setAccessible(true); 
       dataMtd.invoke(mgr, true); 
      } catch (IllegalArgumentException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (NoSuchMethodException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IllegalAccessException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (InvocationTargetException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

我用下面的權限也將我的清單文件:

<uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/> 
    <uses-permission android:name="android.permission.INTERNET" /> 

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> 
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission> 

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" /> 
<uses-permission android:name="android.permission.USE_CREDENTIALS" /> 
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" /> 
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" /> 
<uses-permission android:name="android.permission.READ_SYNC_STATS" /> 
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" /> 
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> 
+0

沒有代碼是很難說任何東西。 – sschrass

回答

0

您需要從connectivityManager對象有IConnectivityManager類的實例,然後使用setMobileDataEnabled ()方法在iConnectivityManager實例上。

嘗試下面的代碼(關於銀河Ý測試(2.3)和Nexus 4(4.2)):

public void cellularState(boolean shouldEnable) { 
    ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 

    try { 
     Class<?> conmanClass = Class.forName(conman.getClass().getName()); 
     Field iConnectivityManagerField = conmanClass.getDeclaredField("mService"); 
     iConnectivityManagerField.setAccessible(true); 
     Object iConnectivityManager = iConnectivityManagerField.get(conman); 

     Class<?> iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName()); 
     Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE); 
     setMobileDataEnabledMethod.setAccessible(true); 

     setMobileDataEnabledMethod.invoke(iConnectivityManager, shouldEnable); 
    } catch(IllegalAccessException e) { 
     e.printStackTrace(); 
    } catch (ClassNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (SecurityException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (NoSuchFieldException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (NoSuchMethodException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IllegalArgumentException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (InvocationTargetException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

在清單中聲明:

<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>

+0

我在Android 4.0.4平板電腦上試過這個,但它不工作 –

+0

它會拋出任何錯誤嗎?順便說一句,你不需要爲此「蘇」。請檢查logcat,看看是否有錯誤爲您的應用程序拋出 –

+0

嗨,IAM沒有得到任何錯誤,也沒有用我的代碼中的「蘇」..因爲我的設備沒有紮根.. –