加载webView使用框架AgenWeb

使用方法
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");
    }
  1. 释放 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();
    }

大概一些用法…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值