4

我想检测强制门户网络并希望将该网络重定向到我自己的 web 视图(其中包含自定义键盘)。

我尝试使用下面的代码,但它不起作用。我正在测试 KitKat

 public class MainActivity extends AppCompatActivity {

    private static final String URL = "http://clients3.google.com/generate_204";
    private WebView web;
    private Network net;
    private CaptivePortal captivePortal;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = getIntent();
        if (ConnectivityManager.ACTION_CAPTIVE_PORTAL_SIGN_IN.equals(intent.getAction())) {
            net = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK);
            captivePortal = intent.getParcelableExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL);
        }
        web = findViewById(R.id.webView);
        textView = findViewById(R.id.info_view);
        textView.setText(R.string.info_starting);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onStart() {
        super.onStart();
        if (net != null && captivePortal != null) {
            ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            cm.bindProcessToNetwork(net);
            final RequestQueue queue = Volley.newRequestQueue(this);

            final WebViewClient webClient = new WebViewClient() {
                @RequiresApi(api = Build.VERSION_CODES.M)
                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
                    textView.setText(R.string.info_portal_injecting);
                    view.evaluateJavascript("document.getElementById('user.username').value='" + PreferenceManager.getDefaultSharedPreferences(MainActivity.this).getString("pref_user_id", "") + "';" +
                            "document.getElementById('user.password').value='" + PreferenceManager.getDefaultSharedPreferences(MainActivity.this).getString("pref_password", "") + "';" +
                            "document.getElementById('aupAccepted').click();" +
                            "document.getElementById('ui_login_signon_button').click();", new ValueCallback<String>() {
                        @Override
                        public void onReceiveValue(String s) {
                        }
                    });
                    captivePortal.reportCaptivePortalDismissed();
                    finish();
                }
            };

            web.getSettings().setJavaScriptEnabled(true);
            web.setWebViewClient(webClient);

            NetworkRequest checkConnectionRequest = new NetworkRequest(Request.Method.GET, URL,
                    new Response.Listener<NetworkResponse>() {
                        @RequiresApi(api = Build.VERSION_CODES.M)
                        @Override
                        public void onResponse(NetworkResponse response) {
                            if (response.headers.containsKey("Location")) { //some portal returns 200 instead of 302
                                web.loadUrl(response.headers.get("Location"));
                            } else {
                                textView.setText(R.string.info_no_portal);
                                captivePortal.reportCaptivePortalDismissed();
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @RequiresApi(api = Build.VERSION_CODES.M)
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            if (error.networkResponse.statusCode == 302) {
                                textView.setText(R.string.info_starting);
                                web.loadUrl(error.networkResponse.headers.get("Location"));
                            } else {
                                captivePortal.reportCaptivePortalDismissed();
                                textView.setText(R.string.info_unknown_error);
                            }
                        }
                    });
            queue.add(checkConnectionRequest);
        }
    }
}

我的另一个问题是我如何在普通网络上测试它,这可能吗?因为现在我没有任何可用的 captiveNetworks。有什么技巧吗?帮助我解决这两个问题。

4

0 回答 0