Android application setting connection type
I'm writing an application for android. My problem is that I want it to force the connection in GPRS and not use wi fi. I have a solution like below, but this causes the crash of the application at start.
ConnectivityManager CM =
(ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
CM.setNetworkPreference(ConnectivityManager.TYPE_MOBILE);
I also added the below setting lines into manifest fi开发者_如何学Pythonle.
uses-permission android:name="android.permission.WRITE_SETTINGS"
uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"
uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"
uses-permission android:name="android.permission.CHANGE_CONFIGURATION"
uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"
Does anyone know the problem, or an answer to set connection type?
My problem is that I want it to force the connection in GPRS and not use wi fi.
That is not possible in Android today, sorry.
You are able to check to see if the Wi-Fi is on, and in Android 2.0 and above you are able to enable or disable the Wi-Fi programmatically. In Android 1.x, the best you can do is tell the user that they must disable it and point them to the Settings page.
/**
* Checks if Wi-Fi is on.
*
* @return true, if Wi-fi is on.
*/
public static boolean isWiFiOn()
{
WifiManager wifi = (WifiManager) MyAccountApplication.getContext().getSystemService(Context.WIFI_SERVICE);
if (wifi == null)
return false;
List<WifiConfiguration> config = wifi.getConfiguredNetworks();
if (config != null)
for (int i = 0; i < config.size(); i++)
{
if (config.get(i).status == WifiConfiguration.Status.CURRENT)
{
return true;
}
}
return false;
}
public static void setWiFi(Context context, boolean enabled)
{
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (wifi != null)
wifi.setWifiEnabled(enabled);
}
精彩评论