背景:
阅读新闻

webview中js调用java函数

  作者: 今日评论: [字体: ]

JAVA部分

this.wvMeaning.addJavascriptInterface(new JavaScriptInterface(this), "Android");
public class JavaScriptInterface
{
Context mContext;

/**
* Instantiate the interface and set the context
*/
JavaScriptInterface(Context c)
{
mContext = c;
}

/**
* Show a toast from the web page
*/
//annotate the callback method with @JavascriptInterface.
@JavascriptInterface
public void showToast(String toast)
{
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}

@JavascriptInterface
public void startFunction()
{
Toast.makeText(TpMeaning.this, "js调用了java函数", Toast.LENGTH_SHORT).show();
runOnUiThread(new Runnable()
{
@Override
public void run()
{
//msgView.setText(msgView.getText() + "\njs调用了java函数");
}
});
}

@JavascriptInterface
public void startFunction(final String str)
{
Toast.makeText(TpMeaning.this, str, Toast.LENGTH_SHORT).show();
runOnUiThread(new Runnable()
{
@Override
public void run()
{
//msgView.setText(msgView.getText() + "\njs调用了java函数传递参数:" + str);
}
});
}
}

HTML部分
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body onload="javascript:window.Android.startFunction('JS调用Android代码');">

来源:
录入日期:[2016/05/05 2:03:00]
收藏 推荐 打印 | 录入:mikebai | 阅读:
文章评论      
正在加载评论列表...
评论表单加载中...