1
我們有一個場景,我們在android應用程序的webview中打開一個URL。問題是沒有彈出窗口會出現在瀏覽器中。一旦網頁被加載,就會有一個按鈕,當點擊時,彈出窗口在屏幕的底部顯示「URL想要訪問位置」,拒絕並允許按鈕。這種行爲在瀏覽器中提到,但是當同一頁面被加載到內部時一個Webview沒有彈出窗口,活動指示器不停地旋轉。Android 4.4.2(api level = 19)Webview問題
在各種博客中看到這與自定義網頁客戶端一起工作。目前完成的實施是如下: -
代碼在活動網頁流量: -
的onCreate -
webView = (WebView) findViewById(R.id.webView_main);
WebSettings webViewSettings = webView.getSettings();
webViewSettings.setJavaScriptEnabled(true); // enable java script
webViewSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webViewSettings.setBuiltInZoomControls(true);
webViewSettings.setLoadWithOverviewMode(true);
webViewSettings.setUseWideViewPort(true);
webView.setWebChromeClient(new CustomWebChromeClient());
final String link = "URL to be accessed";
webView.loadUrl(link);
定製webchrome客戶端實現的是: -
final class CustomWebChromeClient extends WebChromeClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG, "Processing webview url click...");
if (progressbar.isShowing()) {
} else {
progressbar.show();
}
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " + url);
if (progressbar.isShowing()) {
progressbar.dismiss();
}
if (webView.canGoBack()) {
Log.i(TAG, "button enabled");
// btnBack.setVisibility(View.VISIBLE);
// itemBack.setVisible(true);
} else {
Log.i(TAG, "button disbaled");
// btnBack.setVisibility(View.INVISIBLE);
// itemBack.setVisible(false);
}
}
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Log.e(TAG, "Error: " + description);
Toast.makeText(DetailActivity.this, description,
Toast.LENGTH_SHORT).show();
if (progressbar.isShowing()) {
progressbar.dismiss();
}
if (webView.canGoBack()) {
Log.i(TAG, "button enabled");
// btnBack.setVisibility(View.VISIBLE);
// itemBack.setVisible(true);
} else {
Log.i(TAG, "button disbaled");
// btnBack.setVisibility(View.INVISIBLE);
// itemBack.setVisible(false);
}
}
@Override
public boolean onJsAlert(WebView view, String url, String message,
JsResult result) {
super.onJsAlert(view, url, message, result);
final JsResult finalRes = result;
new AlertDialog.Builder(view.getContext())
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new AlertDialog.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
finalRes.confirm();
}
})
.setCancelable(false)
.create()
.show();
return true;
}
@Override
public boolean onJsConfirm(WebView view, String url, String message,
final JsResult result) {
new AlertDialog.Builder(mContext)
.setTitle("App Titler")
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
result.confirm();
}
})
.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
result.cancel();
}
}).create().show();
return true;
}
@Override
public boolean onJsPrompt(WebView view, String url, String message,
String defaultValue, JsPromptResult result) {
// TODO Auto-generated method stub
System.out.println("JS prompt called");
return super.onJsPrompt(view, url, message, defaultValue, result);
}
}
有沒有什麼辦法可以在webview中具有相同的行爲在瀏覽器中?這與android sdk版本有關,如果有的話是否有任何解決方法?
PS: - 使用Android SDK 4.4.2(API級別 - 19)
檢查[這]( http://stackoverflow.com/questions/23503258/android-eclipse-webview-pop-up-not-displaying)出來可能是你需要彈出來的權限。 – BackStabber 2014-09-29 11:40:45
謝謝@BackStabber。問題似乎有所不同。隨着我們在解決這個問題上越來越近,問題將得到更新。 – Amit 2014-09-30 04:07:25