Android 第三方库AgentWeb的使用_android agentweb


AndroidInterface类中



public class AndroidInterface {
private AgentWeb agent;
private Activity context;
public AndroidInterface(AgentWeb agent, Activity context) {
this.agent = agent;
this.context = context;
}

@JavascriptInterface
public void callAndroid(String url) {
//要用 @JavascriptInterface标注
}
}


调用Javascript方法



//调用Javascript中的方法callAndroid
mAgentWeb.getJsAccessEntrace().quickCallJs(“callAndroid”);
事件处理

//手机的事件处理
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (agentWeb.handleKeyEvent(keyCode,event)){
return true;
}
return super.onKeyDown(keyCode, event);
}


点击返回键



@Override
public void onBackPressed() {
if (agentWeb!=null&&agentWeb.getIEventHandler().back()){
Log.i(“callBack”, “go back”);
}else {
Log.i(“callBack”, “finish”);
finish();
}
}


跟随Activity 或Fragment生命周期,释放CPU更省电



@Override
protected void onPause() {
mAgentWeb.getWebLifeCycle().onPause();
super.onPause();
}
@Override
protected void onResume() {
mAgentWeb.getWebLifeCycle().onResume();
super.onResume();
}


创建WebviewClient,用来帮助WebView处理各种通知,请求事件  
 有如下等方法  
 onLoadResource()  
 onPageStart()  
 onPageFinish()  
 onReceiveError()  
 onReceivedHttpAuthRequest()



WebViewClient webViewClient=new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
String title=view.getTitle();//拿到webView的title
if (!TextUtils.isEmpty(title)){
setToolBarTitle(title);
}
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageCommitVisible(WebView view, String url) {
super.onPageCommitVisible(view, url);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request);
}
};


创建WebChromeClient,辅助WebView处理JavaScript的对话框,网站图标,网站title,加载进度等,  
 有如下等方法  
 onCloseWindow(关闭WebView)  
 onCreateWindow()  
 onJsAlert(WebView上alert是弹不出来东西的,需要定制你的WebChromeClient处理弹出)  
 onJsPrompt()  
 onJsConfirm()  
 onProgressChanged()  
 onReceivedIcon()  
 onReceivedTitle()



private WebChromeClient webChromeClient=new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
}
};
//使用是链式调用中加入

agentWeb = AgentWeb.with(this)//传入Activity
.setAgentWebParent(ll_root, new LinearLayout.LayoutParams(-1, -1))//传入AgentWeb 的父控件 ,如果父控件为 RelativeLayout , 那么第二参数需要传入 RelativeLayout.LayoutParams .useDefaultIndicator()//使用默认进度条
.setWebViewClient(webViewClient)//加入webViewClient
.setWebChromeClient(webChromeClient)//加入webChromeClient
.createAgentWeb()
.ready()//重试
.go(“https://www.baidu.com”);


修改AgentWeb默认的背景色



### 更多Android高级工程师进阶学习资料

**进阶学习视频**
![](https://img-blog.csdnimg.cn/img_convert/bac44e459b10a60d2876a1ac08d1c01a.webp?x-oss-process=image/format,png)

附上:我们之前因为秋招收集的二十套一二线互联网公司Android面试真题(含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)

![](https://img-blog.csdnimg.cn/img_convert/f79a86fd4a077c6a0319c4286e2b7ad8.webp?x-oss-process=image/format,png)



**里面包含不同方向的自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中…**



**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化学习资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618156601)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

要这份系统化学习资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618156601)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值