我正在尝试让javascript在Android页面中运行:
http://test.swiss-impulse.com/scratch_card_game/index.html
但我对浏览器有疑问。
这是为webview设置的参数:
webView1.setWebChromeClient(mClient);
webView1.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
webView1.addJavascriptInterface(new JavaScriptInterfaceForTitle(this, mPasswordStrength, webView1), "HtmlViewer");
webView1.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
String cookie = CookieManager.getInstance().getCookie(url);
if (url.equals(getContentUrl() + "addcomment")) {
webView1.stopLoading();
Intent addcommentIntent = new Intent(mContext, AddCommentActivity.class);
addcommentIntent.putExtra("url", previousUrl);
startActivity(addcommentIntent);
} else {
previousUrl = url;
((BaseActivity) mContext).showLoadingDialog();
if (!urlStackList.contains(url)) {
urlStackList.add(url);
}
super.onPageStarted(view, url, favicon);
}
}
@Override
public void onPageFinished(WebView view, String url) {
((BaseActivity) mContext).hideLoadingDialog();
if (url.contains("content/shows/") || url.contains("content/tips/")) {
counter = 0;
view.loadUrl("javascript:window.HtmlViewer.showHTML(document.getElementById('" + mContext.getString(R.string.title_name) + "').innerHTML);");
view.loadUrl("javascript:window.HtmlViewer.showHTML(document.getElementById('name_en').innerHTML);");
view.loadUrl("javascript:window.HtmlViewer.showHTML(document.getElementById('name_zh').innerHTML);");
} else {
mStateBar.setShowTitle(mContext.getString(R.string.tv_drama));
}
super.onPageFinished(view, url);
}
});
webview1.setWebChromeClient(mClient);
webView1.getSettings().setJavaScriptEnabled(true);
webView1.getSettings().setPluginState(PluginState.ON);
webView1.getSettings().setPluginsEnabled(true);
webView1.getSettings().setAppCacheMaxSize(httpCacheSize);
webView1.getSettings().setSupportZoom(false);
webView1.getSettings().setBuiltInZoomControls(false);
webView1.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
我错过了一些参数来使这个工作在Android webview上?我检查了Chrome浏览器,它没有任何问题,所以我确定我错过了什么,找不到什么。
修改
class MyChromeClient extends WebChromeClient {
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
mCustomViewCallback = callback;
mTargetView.addView(view);
mCustomView = view;
mContentView.setVisibility(View.GONE);
mTargetView.setVisibility(View.VISIBLE);
mTargetView.bringToFront();
}
@Override
public void onHideCustomView() {
if (mCustomView == null)
return;
mCustomView.setVisibility(View.GONE);
mTargetView.removeView(mCustomView);
mCustomView = null;
mTargetView.setVisibility(View.GONE);
mCustomViewCallback.onCustomViewHidden();
mContentView.setVisibility(View.VISIBLE);
}
// For Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
TvDramaActivity.this.startActivityForResult(Intent.createChooser(i, "Chooser"), 100);
}
// For Android < 3.0
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
openFileChooser(uploadMsg, "");
}
// For Android > 4.1
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
openFileChooser(uploadMsg, "");
}
@Override
public void onProgressChanged(WebView view, int newProgress) {
}
}
public boolean inCustomView() {
return (mCustomView != null);
}
public void hideCustomView() {
mClient.onHideCustomView();
}
答案 0 :(得分:1)
这对我有用:
在WebView Activity类中:
WebView webView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView1 = (WebView) findViewById(R.id.web);
webView1.setWebChromeClient(new WebChromeClient());
webView1.getSettings().setJavaScriptEnabled(true);
webView1.getSettings().setPluginState(PluginState.ON);
webView1.getSettings().setSupportZoom(false);
webView1.getSettings().setBuiltInZoomControls(false);
webView1.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView1.loadUrl("http://test.swiss-impulse.com/scratch_card_game/index.html");
}
您的布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/web"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
不要忘记在清单文件中添加<uses-permission android:name="android.permission.INTERNET"/>
。
希望这会有所帮助。谢谢。
答案 1 :(得分:0)
您可以尝试以下代码
{{1}}