Webview 面试常见问题,前端面试题及答案

WebSettings webSettings = webView.getSettings();

webView.setWebChromeClient(new WebChromeClient());

webSettings.setJavaScriptEnabled(true)

webSettings.setJavaScriptCanOpenWindowsAutomatically(true);

webView.setWebViewClient(new myWebVliewClient());

关注回调的顺序

webview

下载

webView.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);

}

});

WebViewClient和WebChromeClient的区别

================================

这里有很多资料,中文网站千篇一律,

我看了一下Stack Overflow,下面我比较认可

WebViewClient主要涉及展示内容的方法,可以通过这些方法介入内容的展示,WebChromeClient提供了可以和Activity交互的一些方法,可以将js调用反馈给Activity,或者请求一些native 的资源。

  • WebViewClient

void doUpdateVisitedHistory (WebView view, String url, boolean isReload)

void onFormResubmission (WebView view, Message dontResend, Message resend)

void onLoadResource (WebView view, String url)

void onPageCommitVisible (WebView view, String url)

void onPageFinished (WebView view, String url)

void onPageStarted (WebView view, String url, Bitmap favicon)

void onReceivedClientCertRequest (WebView view, ClientCertRequest request)

void onReceivedError (WebView view, int errorCode, String description, String failingUrl)

void onReceivedError (WebView view, WebResourceRequest request, WebResourceError error)

void onReceivedHttpAuthRequest (WebView view, HttpAuthHandler handler, String host, String realm)

void onReceivedHttpError (WebView view, WebResourceRequest request, WebResourceResponse errorResponse)

void onReceivedLoginRequest (WebView view, String realm, String account, String args)

void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error)

boolean onRenderProcessGone (WebView view, RenderProcessGoneDetail detail)

void onSafeBrowsingHit (WebView view, WebResourceRequest request, int threatType, SafeBrowsingResponse callback)

void onScaleChanged (WebView view, float oldScale, float newScale)

void onTooManyRedirects (WebView view, Message cancelMsg, Message continueMsg)

void onUnhandledKeyEvent (WebView view, KeyEvent event)

WebResourceResponse shouldInterceptRequest (WebView view, WebResourceRequest request)

WebResourceResponse shouldInterceptRequest (WebView view, String url)

boolean shouldOverrideKeyEvent (WebView view, KeyEvent event)

boolean shouldOverrideUrlLoading (WebView view, WebResourceRequest request)

boolean shouldOverrideUrlLoading (WebView view, String url)

  • WebChromeClient

Bitmap getDefaultVideoPoster ()

View getVideoLoadingProgressView ()

void getVisitedHistory (ValueCallback<String[]> callback)

void onCloseWindow (WebView window)

boolean onConsoleMessage (ConsoleMessage consoleMessage)

void onConsoleMessage (String message, int lineNumber, String sourceID)

boolean onCreateWindow (WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg)

void onExceededDatabaseQuota (String url, String databaseIdentifier, long quota, long estimatedDatabaseSize, long totalQuota, WebStorage.QuotaUpdater quotaUpdater)

void onGeolocationPermissionsHidePrompt ()

void onGeolocationPermissionsShowPrompt (String origin, GeolocationPermissions.Callback callback)

void onHideCustomView ()

boolean onJsAlert (WebView view, String url, String message, JsResult result)

boolean onJsBeforeUnload (WebView view, String url, String message, JsResult result)

boolean onJsConfirm (WebView view, String url, String message, JsResult result)

boolean onJsPrompt (WebView view, String url, String message, String defaultValue, JsPromptResult result)

boolean onJsTimeout ()

void onPermissionRequest (PermissionRequest request)

void onPermissionRequestCanceled (PermissionRequest request)

void onProgressChanged (WebView view, int newProgress)

void onReachedMaxAppCacheSize (long requiredStorage, long quota, WebStorage.QuotaUpdater quotaUpdater)

void onReceivedIcon (WebView view, Bitmap icon)

void onReceivedTitle (WebView view, String title)

void onReceivedTouchIconUrl (WebView view, String url, boolean precomposed)

void onRequestFocus (WebView view)

void onShowCustomView (View view, int requestedOrientation, WebChromeClient.CustomViewCallback callback)

void onShowCustomView (View view, WebChromeClient.CustomViewCallback callback)

boolean onShowFileChooser (WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)

JS -> Native 通讯

===============

WebViewClient#shouldOverrideUrlLoading


JS 调用Native

链接方式

webview.setWebViewClient(new WebViewClient(){

@Override

public boolean shouldOverrideUrlLoading(WebView view, String s) {

Uri uri = Uri.parse(s);

Log.d(“test112”, s);

if(uri.getScheme().startsWith(“jsbridge”)) {

String arg1 = uri.getQueryParameter(“arg1”);

String arg2 = uri.getQueryParameter(“arg2”);

String s1 = “JS调用Native,参数1:”+arg1+“参数2:”+arg2;

Toast.makeText(MainActivity.this, s1, Toast.LENGTH_LONG).show();

}

return true;

}

});

@JavascriptInterface


public class AndroidToJS extends Object {

@JavascriptInterface

public void hello(String msg) {

Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();

}

}

webView.addJavascriptInterface(new AndroidToJS(), “test”);

  • 提供用于JS调用的方法必须为public类型

  • 在API 17及以上,提供用于JS调用的方法必须要添加注解@JavascriptInterface

  • 这个方法不是在主线程中被调用的

bridge

WebChromeClient#onJsAlert()、onJsConfirm()、onJsPrompt()



webview.setWebChromeClient(new WebChromeClient() {

@Override

public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {

result.cancle();

return true;

}

});

如果不调用result.cancle(),并且返回false,会展示一个dialog

Native -> JS 通讯

===============

webView.loadUrl("javascript:xxx)


webView.loadUrl(“javascript: alert(‘Native注入的JS’)”);

或者内置js代码

InputStreamReader isr = null;

try {

isr = new InputStreamReader(this.getAssets().open(“test.js”), “UTF-8”);

BufferedReader bf = new BufferedReader(isr);

String content = “”;

StringBuilder sb = new StringBuilder();

while (content != null) {

content = bf.readLine();

if (content == null) {

break;

}

sb.append(content.trim());

}

bf.close();

wholeJS = sb.toString();

} catch (IOException e) {

e.printStackTrace();

}

webView.loadUrl("javascript: " + wholeJS);

evaluateJavascript


  • 20
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值