android 7.0 浏览器 拦截适配 shouldOverrideUrlLoading

Posted 当富网络

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android 7.0 浏览器 拦截适配 shouldOverrideUrlLoading相关的知识,希望对你有一定的参考价值。


浏览器使用第三方开源github框架:

//浏览器 https://github.com/Justson/AgentWeb
compile 'com.just.agentweb:agentweb:1.2.6'

7.0路径拦截存在适配问题shouldOverrideUrlLoading会分别访问不同参数的同一方法:

7.0以上系统访问
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)


7.0以下系统访问
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) 


具体例如下:
public class MessageFragment extends Fragment 
    View view;
    @Bind(R.id.setting)
    TextView setting;
    @Bind(R.id.userInfo)
    TextView userInfo;
    @Bind(R.id.rootPr)
    RelativeLayout rootPr;
    private AgentWeb mAgentWeb;
    public String url;

    private boolean isGoToLogin = false;
    private long currentMillTime = 0;

    public static MessageFragment newInstance() 
        MessageFragment mFragment = new MessageFragment();
        Bundle bundle = new Bundle();
        mFragment.setArguments(bundle);
        return mFragment;
    

    private String addSessionKey(String url) 
        if (StringUtil.isEmpty(url)) return url;
        if (url.indexOf("?") != -1) 
            return url.replaceFirst("\\\\?","?sessionKey=" + SettingPrefUtil.getZHXFSession(getActivity()) + "&");
         else 
            return url += "?sessionKey=" + SettingPrefUtil.getZHXFSession(getActivity());
        
    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
        view = inflater.inflate(R.layout.fragment_message, null);
        ButterKnife.bind(this, view);
        url = MyConfigZHXF.Asset_EX_URL + "index_msg.html";
        url = addSessionKey(url);
        mAgentWeb = AgentWeb.with(getActivity())//传入Activity
                .setAgentWebParent(rootPr, new RelativeLayout.LayoutParams(-1, -1))//传入AgentWeb 的父控件 ,如果父控件为 RelativeLayout , 那么第二参数需要传入 RelativeLayout.LayoutParams
                .useDefaultIndicator()// 使用默认进度条
                .defaultProgressBarColor() // 使用默认进度条颜色
                //.setReceivedTitleCallback(this) //设置 Web 页面的 title 回调
                .setWebViewClient(mWebViewClient)
                .createAgentWeb()
                .ready()
                .go(url);
        //zhxfAddHeaders(url); //智慧信访sessionKey 添加
        return view;
    

    private WebViewClient mWebViewClient=new WebViewClient()
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) 
            String url = request.getUrl().toString();
            if (StringUtil.isEmpty(url)) return true;
            //是否使用第三方浏览器 返回true不调用 返回false调用
            if (dealWithSpecialUrl(url)) return true;
            //return super.shouldOverrideUrlLoading(view,request);
            return true;
        

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) 
            if (dealWithSpecialUrl(url)) return true;
            return true;
            //return super.shouldOverrideUrlLoading(view, url);
        

        /*@Override
        public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) 
            String result = "<html>\\n" +
                    "<title>点击访问</title>\\n" +
                    "<body>\\n" +
                    "<a href=\\"www.lvche.com.cn\\">XX科技</a>,一个为技术研发而生的科技型研发公司\\n" +
                    "</body>\\n" +
                    "<html>";
            WebResourceResponse response = new WebResourceResponse("text/html",
                    "utf-8",
                    new ByteArrayInputStream(result.getBytes()));
            return response;
        */
    ;

    private boolean dealWithSpecialUrl(String url) 
        if (System.currentTimeMillis() - currentMillTime < 1000) 
            currentMillTime = System.currentTimeMillis();
            return true;
        
        if (url.indexOf("index_login") != -1) 
            LoginZHXFActivity.startActivity(getActivity());
            isGoToLogin = true;
            return true;
        
        if (url.indexOf("index_set") != -1) 
            SettingActivity.startActivity(getActivity());
            return true;
        
        if (!StringUtil.isEmpty(url)) 
            BrowserLvcheActivity.startActivity(getActivity(), url);
            return true;
        
        return false;
    

    private void zhxfAddHeaders(String url) 
        //兑吧添加请求头
        Map<String,String> map=new HashMap<>();
        map.put("sessionKey", SettingPrefUtil.getZHXFSession(getActivity()));
        mAgentWeb.getWebCreator().get().loadUrl(url,map);
    

    public void reloadUrl() 
        if (mAgentWeb != null) 
            url = MyConfigZHXF.Asset_EX_URL + "index_msg.html";
            url = addSessionKey(url);
            if (mAgentWeb != null) mAgentWeb.getWebCreator().get().loadUrl(url);
        
    

    @Override
    public void onResume() 
        super.onResume();
        if (mAgentWeb != null && isGoToLogin) 
            reloadUrl();
            isGoToLogin = false;
        
    

    @Override
    public void onStart() 
        super.onStart();
        /*setting.setOnClickListener(new NoDoubleClickListener() 
            @Override
            public void onNoDoubleClick(View v) 
                SettingActivity.startActivity(getActivity());
            
        );
        userInfo.setOnClickListener(new NoDoubleClickListener() 
            @Override
            public void onNoDoubleClick(View v) 
                UserInfoActivity.startActivity(getActivity());
            
        );*/
    

    @Override
    public void onDestroyView() 
        super.onDestroyView();
        ButterKnife.unbind(this);
    


开发者涨薪指南 48位大咖的思考法则、工作方式、逻辑体系

以上是关于android 7.0 浏览器 拦截适配 shouldOverrideUrlLoading的主要内容,如果未能解决你的问题,请参考以下文章

Android 7.0适配

安卓7.0+https抓包新姿势(无需Root)

我们来一探究竟 Android 7.0 适配中 FileProvider 部分

Android项目实战(四十):Andoird 7.0+ 安装APK适配

Android 根据城市获取经纬度 适配Android 7.0 Android 8.0

适配整理Android 7.0 调取系统相机崩溃解决android.os.FileUriExposedException