3

I'm trying to understand the details of the ConnectivityManager. I noticed there are methods setNetworkPreference() and getNetworkPreference(), but there are no details in the documentation about how they should be used, except that they accept/return an integer, respectively. There is also a constant called DEFAULT_NETWORK_PREFERENCE, but that doesn't really provide many hints. Any ideas?

Thanks in advance!

4

2 回答 2

4

你是对的,没有文件。如果您在 android 的源代码中 grep,您会看到以下内容。

user@host:~/android/system/frameworks$ grep -r "setNetworkPreference" *
base/core/java/android/net/IConnectivityManager.aidl:    void setNetworkPreference(int pref);
base/core/java/android/net/ConnectivityManager.java:    public void setNetworkPreference(int preference) {
base/core/java/android/net/ConnectivityManager.java:            mService.setNetworkPreference(preference);
...
base/cmds/svc/src/com/android/commands/svc/WifiCommand.java:                    connMgr.setNetworkPreference(ConnectivityManager.TYPE_WIFI);
base/cmds/svc/src/com/android/commands/svc/DataCommand.java:                    connMgr.setNetworkPreference(ConnectivityManager.TYPE_MOBILE);
base/services/java/com/android/server/ConnectivityService.java:    public void setNetworkPreference(int preference) {

从上面看起来它只是用于设置访问网络的首选方法TYPE_WIFITYPE_MOBILE

于 2012-08-03T11:43:21.570 回答
1

One example to test network :

public static boolean checkStatus(Context context)
{
    final ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

    final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    if( wifi.isAvailable() || mobile.isAvailable())
    {
        return true;
    }
    else
    {
        Log.i(DEBUG_TAG, "No network available");
        return false;
    }

}
于 2012-08-03T11:20:32.850 回答