Android application setting connection type

守給你的承諾、 提交于 2019-12-06 08:05:16

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);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!