2

我正在为android编写一个应用程序。我的问题是我希望它强制连接 GPRS 而不是使用 wi fi。我有一个类似下面的解决方案,但这会导致应用程序在启动时崩溃。

ConnectivityManager CM = 
    (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
CM.setNetworkPreference(ConnectivityManager.TYPE_MOBILE);

我还将以下设置行添加到清单文件中。

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" 

有谁知道问题或设置连接类型的答案?

4

2 回答 2

1

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);
}
于 2011-06-13T19:06:37.223 回答
1

我的问题是我希望它强制连接 GPRS 而不是使用 wi fi。

抱歉,这在今天的 Android 中是不可能的。

于 2010-02-09T16:19:45.483 回答