Passing data from java class to Web View html

前端 未结 5 2170
情书的邮戳
情书的邮戳 2020-11-27 03:52

I\'m loading below html in my webView

https://mail-attachment.googleusercontent.com/attachment/?ui=2&ik=25c0c425c6&view=att&th=138db54ff27a

5条回答
  •  粉色の甜心
    2020-11-27 04:22

    First, your URL seems not available.

    If you want to do data exchange between android app and your web app/web page you can achieve this via javascript.

    Here is an example from Android official site:

    Create a class like this:

    public class JavaScriptInterface {
        Context mContext;
    
        /** Instantiate the interface and set the context */
        JavaScriptInterface(Context c) {
            mContext = c;
        }
    
        /** Show a toast from the web page */
        @JavascriptInterface
        public void showToast(String toast) {
            Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
        }
    }
    

    In your WebView:

    WebView webView = (WebView) findViewById(R.id.webview);
    webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
    

    In your web page:

    
    
    
    

    If you wanna pass something to your webpage, just calling corresponding javascript function:

    String str = "xxx";
    myWebView.loadUrl("javascript:xxx('"+str+"')");
    

    Here is the Reference: http://developer.android.com/guide/webapps/webview.html

提交回复
热议问题