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