使用方法
1.build.gradle中导入
implementation 'com.just.agentweb:agentweb:4.0.2'
implementation 'com.just.agentweb:download:4.0.2' // (可选)
implementation 'com.just.agentweb:filechooser:4.0.2'// (可选)
2.简单使用
@BindView(R.id.cl_main)
CoordinatorLayout clMain;
private AgentWeb agentWeb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView(){
agentWeb = AgentWeb.with(this)
.setAgentWebParent(clMain,new LinearLayout.LayoutParams(-1,-1))
.useDefaultIndicator()
.createAgentWeb()
.ready()
.go("http://www.jd.com");
}
- 释放 CPU 更省电
@Override
protected void onResume() {
super.onResume();
if (mAgentWeb!=null){
mAgentWeb.getWebLifeCycle().onResume();
}
}
@Override
protected void onPause() {
super.onPause();
if (mAgentWeb!=null){
mAgentWeb.getWebLifeCycle().onPause();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mAgentWeb!=null){
mAgentWeb.getWebLifeCycle().onDestroy();
}
}
4.全屏播放视频
<!--如果你的应用需要用到视频 , 那么请你在使用 AgentWeb 的 Activity 对应的清单文件里加入如下配置-->
android:hardwareAccelerated="true"
android:configChanges="orientation|screenSize"
5.与js交互
①android调用js
js端代码
function callByAndroid(){
console.log("callByAndroid")
}
android端调用
mAgentWeb.getJsAccessEntrace().quickCallJs("callByAndroid");
②js调用android
//android端提供方法
public class AndroidInterface {
private AgentWeb agent;
private Context context;
public AndroidInterface(AgentWeb agent, Context context) {
this.agent = agent;
this.context = context;
}
@JavascriptInterface
public String BackToAndroid(){
return "fromAndroid";
}
}
//mAgenWeb调用的方法
mAgentWeb.getJsInterfaceHolder().addJavaObject("android",new AndroidInterface (mAgentWeb,this));
js端调用
window.android.BackToAndroid();
6.返回上一页
if (!mAgentWeb.back()){
AgentWebFragment.this.getActivity().finish();
}
大概一些用法…