JavaScript的不使用addJavascriptInterface在Android中调用方法 [英] JavaScript not calling method in Android using addJavascriptInterface
问题描述
我已搜查高和低,发现这这样的例子很多,但无法得到它的工作,我的设置是目前:
I have searched high and low and found so many examples of this, but unable to get it to work, my setup currently is:
Notifier.java
public class Notifier{
Context mContext;
Notifier(Context c) {
mContext = c;
}
@JavascriptInterface
public void showText()
{
Toast.makeText(mContext, "Some text!", Toast.LENGTH_LONG).show();
}
}
SearchLicenseActivity.java
public class SearchLicenseActivity extends Activity {
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_license);
LoadSearch();
}
public void LoadSearch(){
webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new Notifier(this), "Android");
webView.loadUrl("javascript:Android.showText();");
}
}
所以我期待举杯显示。这可能是值得一提的是点击了previous活动的一个按钮时,这个SearchActivity被创建;所以我想它来执行LoadSearch直线距离,并从JavaScript中的吐司。
So I am expecting a Toast to show. It may be worth noting that this SearchActivity gets created when a button on a previous Activity is clicked; so I want it to execute LoadSearch straight away and get the Toast from the JavaScript.
我希望你们中的一个在那里能治好我的困境在这个!
I hope one of you out there can cure my woes over this!
编辑:我也没有得到在LogCat中的任何错误
I am also not getting any errors in LogCat.
推荐答案
OK,原来web视图必须加载的网页。
OK, it turns out webView must have a webpage loaded.
即使URL为关于:空白这似乎工作。所以......
Even if the url is "about:blank" this seems to work. So...
webView.loadUrl("about:blank");
webView.loadUrl("javascript:Android.showText();");
...作品。
这篇关于JavaScript的不使用addJavascriptInterface在Android中调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!