Android Webview in use javascript to call pre-defined Java function

 1. First define what a class designed to have a javascript call

public class JavaScriptInterface {
    // share your news
    public void share(String newsImageUrl, String newsTitle, String newsUrl) {
        Intent intent = new Intent(ActivityHomeRang.this,
                ActivityShare.class);
        Bundle news = new Bundle();
        news.putString("ImageUrl", newsImageUrl);
        news.putString("Title", newsTitle);
        news.putString("Url", newsUrl);
        intent.putExtras(news);
        startActivity(intent);
    }
}

 

2. Then you need to listen to the WebView, add to it the external event listener

WebView testView;
...
testView.addJavascriptInterface(new JavaScriptInterface(), "android");

 

3. calling code server pages

function callAndroidShare() {
    android.share('http://www.test.com', 'test title', 'http://www.test.com/news.aspx');
}

 

Reproduced in: https: //www.cnblogs.com/davidgu/p/3950917.html

Guess you like

Origin blog.csdn.net/weixin_33937913/article/details/93802630