I'm trying to get a value of 0 or 1 if the class 'logout' is present in my webview. (the base idea was that if i find the 'logout' class it means that the user is loged in and that's what i need to know)

如果我的webview中存在类“注销”,我试图获得0或1的值。 (基本的想法是,如果我找到'注销'类,这意味着用户已经进入,这就是我需要知道的)

So far i have searched and come to something like this.

到目前为止,我已经搜索并得出类似的结果。

the JavscriptInterface() class

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);
        }
}

in 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 is a string with my web address

URL是包含我的网址的字符串

So far the result is -1. What im i writing wrong?

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

1 个解决方案

#1


0

Try this one :

试试这个:

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();
    }

}

and this xml:

这个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>

更多相关文章

  1. String字符串的应用
  2. java 正则表达式查找某段字符串中所有小写字母开头的单词并统计
  3. Javascript 检查字符串是否是数字的几种方法
  4. JavaScript替换字符串中最后一个字符
  5. Java中怎么把字符串数组转为整形数组
  6. java里如何取出一个字符串中的数字?
  7. Java解析Json字符串--Map
  8. java 中判断一个字符串中大小写字母的个数及其思路
  9. java7 switch语句使用字符串

随机推荐

  1. android之ListView布局
  2. 安卓手机常见名词解释
  3. MonkeyRunner与模拟器连接
  4. Android应用程序四大组件
  5. Android的布局控件----LinearLayout(线性
  6. Android 在 xml中定义图片
  7. android设置一个通用的控件,比如返回按钮
  8. Android:ImageView如何显示网络图片
  9. 实现ListView的item逐个飞入效果——Layo
  10. button imageButton 背景透明