Yhzhtk / note

知识代码笔记
https://github.com/Yhzhtk/note/issues
MIT License
108 stars 11 forks source link

Android开发自定义浏览器,对特定URL请求进行捕捉和内容替换 #1

Open Yhzhtk opened 11 years ago

Yhzhtk commented 11 years ago
package yh.andr;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Locale;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * @author gudh
 * 自定义浏览器Client
 */
public class MyWebViewClient extends WebViewClient {

    private Activity act;

    public MyWebViewClient(Activity act){
        this.act = act;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        System.out.println(url);
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onLoadResource(WebView view, String url) {
        super.onLoadResource(view, url);
    }

    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view,
            String url) {
        System.out.println("loadRes: " + url);
        return getResource(url);
    }

    public WebResourceResponse getResource(String url){
        WebResourceResponse res = null;
        if (url.toLowerCase(Locale.getDefault()).endsWith(".png")) { // 替换图片
            System.out.println("load jpg:" + url);

            BitmapDrawable in = (BitmapDrawable) act.getResources().getDrawable(R.drawable.ic_launcher);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            in.getBitmap().compress(Bitmap.CompressFormat.PNG, 100,
                    stream);
            ByteArrayInputStream bis = new ByteArrayInputStream(stream
                    .toByteArray());

            res = new WebResourceResponse("image/png", "UTF-8", bis);
        }else if(url.contains(".cn")){ // 替换网页
            System.out.println("have cn: " + url);
            try {
                String html = "<html><title>已捕捉<title><body><h1>该页面已被替换</h1></body></html>";
                ByteArrayInputStream bis = new ByteArrayInputStream(html.getBytes("utf-8"));
                res = new WebResourceResponse("text/html", "UTF-8", bis);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return res;
    }
}
Yhzhtk commented 11 years ago

WebViewClient主要帮助WebView处理各种通知、请求事件的,比如:

onLoadResource onPageStart onPageFinish onReceiveError onReceivedHttpAuthRequest

WebChromeClient主要辅助WebView处理Javascript的对话框、网站图标、网站title、加载进度等比如:

onCloseWindow(关闭WebView) onCreateWindow() onJsAlert (WebView上alert无效,需要定制WebChromeClient处理弹出) onJsPrompt onJsConfirm onProgressChanged onReceivedIcon onReceivedTitle

看上去他们有很多不同,实际使用的话,如果你的WebView只是用来处理一些html的页面内容,只用WebViewClient就行了,如果需要更丰富的处理效果,比如JS、进度条等,就要用到WebChromeClient。

如果你需要所有功能的话,那么把两个都加上吧。

Pinned commented 7 years ago

shouldInterceptRequest 只是在API 11 过后才加上,如果要在API 11以前进行类容替换,应该如何处理?