繁体   English   中英

如何从WebView到android获取javascript值?

[英]How can i get a javascript value from webview to android?

如果我的网络视图中存在类“注销”,我试图获取0或1的值。 (基本思想是,如果我找到“注销”类,则意味着用户已登录,这就是我需要知道的)

到目前为止,我已经搜索并得出类似这样的信息。

JavscriptInterface()类

private static class JavascriptInterface{
    private Map<String,String> valueMap = new HashMap<String,String>();
        public String set(String key, String value){
            valueMap.put(key,  value);
            return "";
        }

        public String get(String key){
            return valueMap.get(key);
        }
}

在onCreate()中

webview_mycare = (WebView) findViewById(R.id.webview_mycare);
webview_mycare.getSettings().setJavaScriptEnabled(true);
js = new JavascriptInterface(); 
webview_mycare.addJavascriptInterface(js, "jsinterface");
js.set(JS_KEY, "-1");

    webview_mycare.loadUrl(
                    "javascript:document.function hasClass(element, cls){ return(' '+ element.className +' ').indexOf(' '+ cls +' ')>-1;"
                  + "javascript:document.function var el = document.getElementsByClassName('front-page');"
                  + "javascript:document.function if(hasClass(el,'logout')==true){ "
                  +         "window.jsinterface.set('"+JS_KEY+"','1');"
                  +     "}else{"
                  +         "window.jsinterface.set('"+JS_KEY+"','0');"
                  +     "}"
                );
    try{
        Thread.sleep(200);
    }catch(InterruptedException ex){

        }

    String v=js.get(JS_KEY);
    System.out.println("isMapPage: getValue: value="+v);  
    if("1".equals(v)){  
        Log.e(">>>>>>>>>>>>", "JS_KEY "+ v);
    }else{
        Log.e(">>>>>>>>>>>>", "JS_KEY "+ v);
    }

    webview_mycare.loadUrl(URL);

URL是我的网址的字符串

到目前为止,结果是-1。 我写错了什么?

试试这个:

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.annotation.SuppressLint;
import android.app.Activity;

public class MainActivity extends Activity implements OnClickListener {

    private static final String URL = "file:///android_asset/index.html";
    private WebView mWebView;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mWebView = (WebView) findViewById(R.id.webview); 
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setWebChromeClient(new WebChromeClient());
        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                String user = ((EditText) findViewById(R.id.edit_text)).getText().toString();
                if (user.isEmpty()) {
                    user = "World";
                }
                String javascript="javascript: document.getElementById('msg').innerHTML='Hello "+user+"!';";
                view.loadUrl(javascript);
            }
        });
        refreshWebView();
        findViewById(R.id.button).setOnClickListener(this);
    }

    private void refreshWebView() {
        mWebView.loadUrl(URL);
    }

    @Override
    public void onClick(View v) {
        refreshWebView();
    }

}

和这个XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:layout_gravity="center"
    android:orientation="vertical">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/edit_text"
        android:hint="What is your name?" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:text="Update Web View"/>

    <WebView 
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
        android:layout_weight=".5"
       android:id="@+id/webview" />

</LinearLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM