首页 > 移动平台 > 详细

Android中Webview使用javascript调用事先定义好的Java函数

时间:2014-09-02 11:45:44      阅读:365      评论:0      收藏:0      [点我收藏+]

 1. 首先定义好一个类,专们用于给javascript调用

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. 然后需要监听你的WebView, 给它加上对外的事件监听

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

 

3. 服务端网页的调用代码

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

 

Android中Webview使用javascript调用事先定义好的Java函数

原文:http://www.cnblogs.com/davidgu/p/3950917.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!