Android Tv wifi网络登录认证

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_30996881/article/details/101370301

Android Tv wifi网络登录认证

前言

现在商铺、酒店等公共场所的wifi 网络大多数都需要进行网络登录认证,但在android Tv上面系统设置上的wifi模块好像没有对此场景做出处理,必须要Tv上面安装了浏览器才能进行登录认证,为了解决用户的体验,所以在wifi模块添加自动弹出网络登录认证功能是很有必要的。

网络认证的步骤

  1. 连接wifi成功后请求服务器进行判断是否需要网络登录认证。

  2. 需要网络登录认证,打开webView 调转到网络认证登录网站。

1.判断wifi是否需要网络登录认证。

android 默认的请求服务器有2个 (前提是手机厂商没有修改这个默认的服务器)。

  1. http://connectivitycheck.gstatic.com/generate_204
  2. http://www.google.com/gen_204

对服务器进行请求,获取数据是否需要认证:如果返回204则不需要认证网络。

  /**
     * 判断wifi是否需要Portal认证
     *
     * @return true  false
     */
    public static boolean getWifiSetPortal(String Url) {
        int WALLED_GARDEN_SOCKET_TIMEOUT_MS = 2500;
        HttpURLConnection urlConnection = null;
        int code = 0;
        try {
            URL url = new URL(Url);
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setInstanceFollowRedirects(false);
            urlConnection.setConnectTimeout(WALLED_GARDEN_SOCKET_TIMEOUT_MS);
            urlConnection.setReadTimeout(WALLED_GARDEN_SOCKET_TIMEOUT_MS);
            urlConnection.setUseCaches(false);
            urlConnection.getInputStream();
            code = urlConnection.getResponseCode();
            return code != 204;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }
    }

如果需要稳妥一点话,可以对2个服务器依次进行请求。

  public static boolean isWifiSetPortal() {
        if (!getWifiSetPortal(DEFAULT_HTTP_URL)) {
           return !getWifiSetPortal(DEFAULT_FALLBACK_URL);
        }
        return false;
    }

2.需要网络登录认证,打开webView 调转到网络认证登录网站。

/**
 * webView 网络认证界面
 */

public class CaptivePortalLoginActivity extends Activity {

    private WebView mWifiLoginWeb;
    private String url = "http://www.baidu.com/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_portal);
        mWifiLoginWeb = (WebView) findViewById(R.id.wifi_login_web);
        setWeb();
        mWifiLoginWeb.loadUrl(url);
    }

    private void setWeb() {
        final WebSettings webSettings = mWifiLoginWeb.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        mWifiLoginWeb.setVisibility(View.VISIBLE);
        webSettings.setSupportZoom(true);
        webSettings.setDomStorageEnabled(true);
        mWifiLoginWeb.requestFocus();
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setSupportZoom(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        mWifiLoginWeb.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.d("setweb", url);
                return false;
            }
        });
    }

}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <WebView
        android:id="@+id/wifi_login_web"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </WebView>
</FrameLayout>

启动wifi 登录activity之后,会自动的跳转到网络登录界面,然后用户需要输入用户名和秘密点击登录。

猜你喜欢

转载自blog.csdn.net/qq_30996881/article/details/101370301