Android webview对数据的访问限制

时间:2013-08-13 06:51:52

标签: android webview

我目前正在使用网页浏览来显示本地HTML内容,但此内容可能会引用在线图片。我需要的是在用户使用3G / 4G时防止下载此在线数据。我只想让webview只能通过Wifi访问互联网。 我没有找到任何办法这样做......有可能吗?

1 个答案:

答案 0 :(得分:1)

要处理要加载的URL和不加载的URL: -

“将WebView客户端与您的Web视图一起使用并覆盖此方法shouldOverrideUrlLoading,如果它不是您的本地URL使用不同的状态和条件,则不会调用任何URL  根据您在该方法中的要求“


决定使用Wi-Fi还是3G / 4G: - 使用下面的代码,您可以检测手机是通过wifi还是数据连接,并且可以相应地运行:

            public static boolean isDataEnabled(Context context)
            {
                return (isMobileDataEnabled(context) || isWifiEnabled(context));
            }


            public static boolean isMobileDataEnabled(Context context)
            {
                if(context == null)
                    return false;
                ConnectivityManager connectivityManager = (ConnectivityManager)
                    context.getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo networkInfo = null;
                if (connectivityManager != null) {
                    networkInfo =
                        connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                }
                return networkInfo == null ? false : networkInfo.isConnected();
            }

            public static boolean isWifiEnabled(Context context)
            {
                if(context == null)
                    return false;
                ConnectivityManager connectivityManager = (ConnectivityManager)
                    context.getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo networkInfo = null;
                if (connectivityManager != null) {
                    networkInfo =
                        connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                }
                return networkInfo == null ? false : networkInfo.isConnected();
            }


    //Chceking user preference:

        public static boolean shouldUseOnlineServices(Context context)
        {
            if(context == null || isDataEnabled(context) == false)
                return false;

            SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
            if(pref != null)
            {
                //String wifi = pref.getString("pref_camera_when_to_use_online_services","1");
                //boolean onlyOnWifi = (wifi.equals("0")== true) ? false:true;
                boolean onlyOnWifi = pref.getBoolean("pref_radio_wifi", false);
                if(onlyOnWifi == false)
                    return true;
                if(isWifiEnabled(context) == true)
                    return true;
                else
                    return false;
            }

            if(isWifiEnabled(context) == true)
                return true;
            else
                return false;
        }