webView.loadUrl("https://www.baidu.com/");
public class WebViewActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
webView=findViewById(R.id.webView);
webView.loadUrl("https://www.baidu.com");
webView.setWebViewClient(new WebViewClient());
}
}
去访问手机里面的sd卡中的文件
webView.loadUrl("file://"+ Environment.getExternalStorageDirectory().getPath()+"/1/index.html");
去访问安卓资源
webView.loadUrl("file:///android_asset/index.html");
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
Map<String,String>reqHeader=new HashMap<>();
reqHeader.put("android-webview-demo","textRequest");
webView.loadUrl("http://172.26.48.1:8080",reqHeader);
return super.shouldOverrideUrlLoading(view, request);
}
});
放入请求头部
webView.loadData("Hello World
","text/html","utf-8");
打印出HelloWorld
webView.loadDataWithBaseURL("http://www.imooc.com","","text/html","utf-80",null);
@Override
protected void onPause() {
super.onPause();
//停止webview
webView.onPause();
//停止所有webview
webView.pauseTimers();
}
@Override
protected void onResume() {
super.onResume();
//开始webview
webView.onResume();
//开始所有webview
webView.resumeTimers();
}
不及时停止可能造成内存泄漏
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
支持缩放
//支持缩放
settings.setSupportZoom(true);
//支持网页内置原生缩放工具
settings.setBuiltInZoomControls(true);
支持安卓原生缩放工具
settings.setDisplayZoomControls(true);
反正 shouldInterceptRequest 肯定在onLoadResource之前
onpagestart和shouldInterceptRequest不一定
shouldInterceptRequest返回为空时候是webview自己去处理 如果我们想要处理就
@Nullable
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
String url = request.getUrl().toString();
WebResourceResponse webResourceResponse = super.shouldInterceptRequest(view, request);
if ("http://www.baodu.com".equals(url)){
return new WebResourceResponse("text/html","utf-8",null);
}
return webResourceResponse;
}
告诉宿主应用程序加载页的当前进度。
public void onProgressChanged(WebView view, int newProgress) {}
将文档标题的更改通知宿主应用程序。
public void onReceivedTitle(WebView view, String title) {}
通知宿主应用程序当前页的新favicon。
public void onReceivedIcon(WebView view, Bitmap icon) {}