对于webView进行交互时,需要注意的几点:
1.漏洞:ADI16之前存在远程代码执行的安全漏洞,使攻击者利用反射机制,调用API调用对象。
2.webview 写在其他容器当中容易造成内存泄漏,我们先把容器中的webview,Remove调,然后去调用webview中的RemoveAllViews,webViewDestory方法区销毁,才不会造成内存泄漏。
3.代码核心如下:
一、java调用js操作:
1.布局文件:
<WebView
android:id="@+id/web"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
2.获取一个网页:
//web显示一个网页 :
web.loadUrl("https://www.baidu.com/");
//web显示一个本地网页 :
web.loadUrl(" file:///android_asset/hi.html");
3.对webview属性操作:
/**
* 设置web:
*/
WebSettings settings = web.getSettings();
settings.setJavaScriptEnabled(true);//支持javaScript
settings.setSupportZoom(true);//缩放
settings.setUseWideViewPort(true);//屏幕自适
4.跳转:
//跳转:
web.setWebViewClient(new WebViewClient(){
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Toast.makeText(MainActivity.this, "网页开始加载", Toast.LENGTH_SHORT).show();
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("baidu")){
web.loadUrl("https://blog.csdn.net/u012124438/article/details/53371102");
}
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Toast.makeText(MainActivity.this, "网页结束", Toast.LENGTH_SHORT).show();
}
});
5.返回上一步:
//覆盖系统的回退键 :
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && web.canGoBack()){
web.goBack();
return true;
return super.onKeyDown(keyCode, event);
}
6.不在原本webview中操作:
web.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(android.webkit.WebView view, WebResourceRequest request) {
web.loadUrl(String.valueOf(request));
return super.shouldOverrideUrlLoading(view, request);
}
});
web.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(android.webkit.WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
if(newProgress == 0){
}else if(newProgress == 100){
}
}
});
二、js调用java
1.assets下创建一个html文件:
<!DOCTYPE html>
<html>
<body>
<input
type="button"
value="js调用java进行吐司"
onclick="myObj.showToast('调用成功');"
/>
<input
type="button"
value="js调用java进行展示列表"
onclick="myObj.shouList();"
/>
</body>
</html>
2.创建一个调用方法的具体类:
public class MyObject {
//设置上下文环境:
Context context;
public MyObject(Context context) {
this.context = context;
}
//设置类的暴露接口:
@JavascriptInterface
public void showToast(String name){
Toast.makeText(context, name+"Leslie", Toast.LENGTH_SHORT).show();
}
@JavascriptInterface
public void showList(){
new AlertDialog.Builder(context)
.setTitle("Missing_Stars")
.setIcon(R.drawable.a)
.setItems(new String[]{"Leslie","Anita","Danay"},null)
.setPositiveButton("Yes",null)
.create()
.show();
}
}
3.主类进行实现调用
public class Ndk_five extends Fragment {
@BindView(R.id.webview)
WebView webview;
Unbinder unbinder;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.ndk_5, null);
unbinder = ButterKnife.bind(this, view);
WebSettings settings = webview.getSettings();
settings.setSupportZoom(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setJavaScriptEnabled(true);
//使用的是Chrome浏览器,只有使用Chrome浏览器才可以弹出HTML中自己的对话框:
webview.setWebChromeClient(new WebChromeClient());
webview.loadUrl("file:///android_asset/work.html");
webview.addJavascriptInterface(new MyObject(getActivity()),"myObj");
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
}