2012-08-03 29 views
1

。如果無線網絡連接沒有任何解決方案禁用所有連接的(即移動)如何禁用其他連接的可用

如果WIFI可用。

+0

它由框架自動控制(即,如果可用的話,WiFi總是被設置爲默認連接)。所以你不必擔心它。 – waqaslam 2012-08-03 09:48:49

+0

我想每次只使用一個連接。我想要禁用移動GSM。如果WiFi在我的設備(SGIII)上有 – 2012-08-03 09:53:29

+0

,它是由Android自動完成的,您不必執行任何操作。 – 2012-08-03 09:56:13

回答

2

試試這個:

ConnectivityManager connManager = (ConnectivityManager) 
getSystemService(CONNECTIVITY_SERVICE); 
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 

if (mWifi.isConnected()) 
{ 
    turnData(false); 
} 
void turnData(boolean ON) throws Exception 
{ 

if(bv == Build.VERSION_CODES.FROYO) 
{ 

Log.i("version:", "Found Froyo"); 
try{ 
    Method dataConnSwitchmethod; 
    Class telephonyManagerClass; 
    Object ITelephonyStub; 
    Class ITelephonyClass; 
    TelephonyManager telephonyManager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE); 

    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 (ON) { 
    dataConnSwitchmethod = ITelephonyClass.getDeclaredMethod("enableDataConnectivity"); 

} else { 
    dataConnSwitchmethod = ITelephonyClass.getDeclaredMethod("disableDataConnectivity"); 
} 
dataConnSwitchmethod.setAccessible(true); 
dataConnSwitchmethod.invoke(ITelephonyStub); 
}catch(Exception e){ 
     Log.e("Error:",e.toString()); 
    } 
else 
{ 
Log.i("version:", "Found Gingerbread+"); 
final ConnectivityManager conman = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE); 
    final Class conmanClass = Class.forName(conman.getClass().getName()); 
final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService"); 
iConnectivityManagerField.setAccessible(true); 
final Object iConnectivityManager = iConnectivityManagerField.get(conman); 
final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName()); 
final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE); 
setMobileDataEnabledMethod.setAccessible(true); 
setMobileDataEnabledMethod.invoke(iConnectivityManager, ON); 
    } 
} 

不要忘了在清單文件中添加這些權限;

android.permission.ACCESS_WIFI_STATE 
android.permission.UPDATE_DEVICE_STATS 
android.permission.CHANGE_NETWORK_STATE 
android.permission.ACCESS_NETWORK_STATE 
android.permission.MODIFY_PHONE_STATE 
android.permission.READ_PHONE_STATE