问题描述
在我的应用程序,我想提供可供选择的Wi-Fi / GPRS的网络连接到Web服务器的选项的用户。可能是回答以下几个疑问句解决我的概率...1.如何检查这是使当前的默认网络连接选项。2.如何在用户选择启用Wi-Fi / GPRS或(如果用户选择GPRS禁用无线网络 - 如果将被要求仅此选项,GPRS工作)
In my app i want to provide the user with the option to choose wi-fi / GPRS for network connectivity to the web server. May be answers to the following ques solve my prob ... 1. How to check which is the current default network connectivity option enabled.2. How to enable wi-fi/GPRS on user selection or (disable the wi-fi if user chooses GPRS - if only this option will be required for GPRS to work)
或者是周围有一些其他的方式来做到这一点?
or is there some other way around to do this ?
推荐答案
试试这个:
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
}
不要忘了在清单文件中添加这些权限;
Don't forget to add these permissions in the manifest file;
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
要启用或禁用无线网络,使用 mWiFi.setWifiEnabled(TRUE | FALSE)
To enable or disable Wifi, use mWiFi.setWifiEnabled(true|false)
要启用/禁用GPRS / 3G,使用下面的code段。
To enable/disable GPRS/3G, use the following code snippet.
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);
}
}
这篇关于如何提供选项,选择Wi-Fi或GPRS为Android应用程序的网络连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!