从android webview调用javascript函数? [英] Calling javascript function from android webview?
本文介绍了从android webview调用javascript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试直接从我的应用程序(webview.apk)调用javascript函数,为了启动一个自动播放html5视频剪辑的脚本,我试图在webview loadURL之后立即添加它,但没有运气。
I try to call a javascript function from directly form my application (webview.apk), in order to start a script which would autoplay a html5 video clip, I have tried to add itt right after webview loadURL but wwithout luck.
也许有人可以看看代码。我错过了什么?谢谢!
Maybe someone could take a look at the code. What am I missing? Thanks!
package tscolari.mobile_sample;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebChromeClient;
import android.media.MediaPlayer;
public class InfoSpotActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
WebSettings webSettings = mainWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mainWebView.setWebChromeClient(new WebChromeClient());
mainWebView.setWebViewClient(new MyCustomWebViewClient());
mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mainWebView.loadUrl("http://server.info-spot.net");
mainWebView.loadUrl("javascript:playVideo()");
}
private class MyCustomWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
推荐答案
等待页面加载完成,然后执行webview.loadUrl(javascript:playVideo());
Wait til the page loading is finished and then execute webview.loadUrl("javascript:playVideo()");
private class MyCustomWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:playVideo()");
}
}
这篇关于从android webview调用javascript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文