在我的应用程序中,我想为用户提供选择wi-fi / GPRS以便与网络服务器建立网络连接的选项。可能是以下问题的答案解决我的问题... 1.如何检查启用的当前默认网络连接选项。 2.如何在用户选择时启用Wi-Fi / GPRS或(如果用户选择GPRS,则禁用Wi-Fi - 如果GPRS工作只需要此选项)
还是有其他方法可以做到这一点吗?
答案 0 :(得分:14)
试试这个:
ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected())
//if wifi connected
}
ConnectivityManager connManager1 = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mMobile = connManager1.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mMobile.isConnected()) {
//if internet connected
}
不要忘记在清单文件中添加这些权限;
android.permission.CHANGE_WIFI_STATE
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
要启用或停用Wifi,请使用mWiFi.setWifiEnabled(true|false)
要启用/禁用GPRS / 3G,请使用以下代码段。
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);
}
}
答案 1 :(得分:3)
您可以使用以下代码块在屏幕上为用户提供选项....
public static ShowAvailable()
{
ConnectivityManager connectivityMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] nwInfos = connectivityMgr.getAllNetworkInfo();
for (NetworkInfo nwInfo : nwInfos)
{
Log.d(TAG, "Network Type Name: " +
nwInfo.getTypeName()); Log.d(TAG, "Network available: " +
nwInfo.isAvailable()); Log.d(TAG, "Network c_or-c: " +
nwInfo.isConnectedOrConnecting()); Log.d(TAG, "Network connected: "
+ nwInfo.isConnected());
}
}
答案 2 :(得分:1)
继续@rIHaN JiTHiN答案,应该注意两个权限android.permission.UPDATE_DEVICE_STATS
和android.permission.MODIFY_PHONE_STATE
仅授予系统应用程序(因为它最初是在Eclipse或Android Studio中设置的)。因此,如果有人遇到此问题,请查看this solution。