如何在Android中自动登录以打开需要Web用户/密码的WiFi?

时间:2014-08-03 11:08:41

标签: android networking login wifi

首先,我连接到开放的Wifi programaticaly

public static boolean conectar(Context ctx, String user, String pass){

    boolean conectado = false;

    String networkSSID = "wifiguay.es";

    WifiConfiguration conf = new WifiConfiguration();
    conf.SSID = "\"" + networkSSID + "\"";   

    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

    WifiManager wifiManager = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE); 
    wifiManager.addNetwork(conf);

    List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
    for( WifiConfiguration i : list ) {
        if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
             wifiManager.disconnect();
             wifiManager.enableNetwork(i.networkId, true);
             wifiManager.reconnect();               
             conectado = true;

             break;
        }           
    }

    return conectado;
}

然后,我检测到打开的Wifi是否为“Wifiguay.es”,然后打开浏览器输入用户名和密码。

public class ConexionEstablecida extends BroadcastReceiver  {

    public void onReceive(Context context, Intent intent) {

        NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        if(info != null){
            if(info.isConnected()){
                WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                String ssid = wifiInfo.getSSID();
                String networkSSID = "wifiguay.es";

                if(ssid.equalsIgnoreCase("\"" + networkSSID + "\"")){
                    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://wifiguay.lan/login?dst=http%3A%2F%2Fwww.google.es%2F"));    
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(i);
                }
            }
        }
    }
}

现在,我不知道如何处理数据。

1 个答案:

答案 0 :(得分:0)

您可以查看this问题,您必须根据需要对其进行自定义,即更改字段名称。

此外,我不知道这是否适用于您的情况,但存储敏感密码是“棘手的”#34;充其量,也许只需打开浏览器并让用户(或浏览器,如果它的设置保存凭据)负责输入密码。

编辑:该问题的示例:

mWebView.loadUrl(url);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
    String user="u";
    String pwd="p";
    view.loadUrl("javascript:document.getElementById('nameofyourusernamefield').value = '"+user+"';document.getElementById('nameofyourpasswordfield').value='"+pwd+"';");
}

});