Example of Android WebView AD filtering

Source: Internet
Author: User

Now most of the Android platform's browsers have the function of advertising filtering, while most sites have ads. Advertising industry is a profitable industry, in my opinion, now most of the Internet products, there are generally two kinds of profit-making model: free ads and members without ads, most of them (including Web sites and apps, etc.) profit source is advertising. As a user, the advertising is very objectionable, but the Internet services we can enjoy free and support these services to continue and grow but benefit from advertising.

Youku Ads

Baidu a bit, the Internet unexpectedly does not have the Android browser screen ads code to achieve, so the guests can only do their own hands. The ads on the Web page is generally webmaster embedded in a section of JS code, want to screen ads only need to block out these JS.

Several callback functions for webviewclient

To implement some advanced operations on WebView, first learn the webviewclient usage, which is webview several common callback functions

1, public boolean shouldoverrideurlloading (webview view, String URL): Click on the request is the link is not called, Overriding this method returns true to indicate that clicking on a link in a Web page is a jump in the current WebView and does not jump to the browser.
2. public void Onreceivedsslerror (WebView view, Sslerrorhandler handler, android.net.http.SslError error):
Overriding this method allows WebView to process HTTPS requests.
3, public boolean shouldoverridekeyevent (WebView view, KeyEvent event): Override this method to handle key events in the browser.
4, public void Onloadresource (webview view, String URL): When a page resource is loaded, the load of each resource (such as a picture) is invoked once.
5. public void onpagestarted (webview view, String URL, Bitmap favicon): Called at the start of the page load.
6, public void onpagefinished (webview view, String URL): Called at the end of the page load.
First look at the above callback function, extremely guests found a huge pit, interception ads is to intercept the load ads JS, the above Onloadresource seems to be a very appropriate function, as long as the onloadresource of the parameters of the URL is loaded ads JS can, If it is not an ad-related URL normal load, if it is not loaded. But it didn't work out after using Onloadresource.

Here's a reference to webviewclient another callback function: Public webresourceresponse shouldinterceptrequest (webview view, String URL)

There are two types of overloads for Shouldinterceptrequest.

Public Webresourceresponse shouldinterceptrequest (webview view, String URL) introduced from API 11, API 21 deprecated
Public Webresourceresponse shouldinterceptrequest (webview view, webresourcerequest request) has been introduced from API 21
Here the very guests use Shouldinterceptrequest (webview view, String URL) to complete the interception of WebView ads.

Intercepting AD resource URLs

The Shouldinterceptrequest function is called back when WebView loads the resource, and we can process the request for the WebView resource by overriding the Shouldinterceptrequest function. Returns the data after processing. If the data returned by the main program is Null,webview, the Network Load resource is requested by itself. There's a hole here: not the Shouldinterceptrequest function returns NULL to block out requests! The correct way to mask requests:

@Override
Public Webresourceresponse shouldinterceptrequest (webview view, String URL) {
url = url.tolowercase ();
if (! Adfiltertool.hasad (context, url)) {
Return super.shouldinterceptrequest (view, URL);/normal load
}else{
return new Webresourceresponse (null,null,null);//contain advertisement resource mask request
}
}

The following is the noadwebviewclient of the most guest-written shielding ads: simply use Webview.setwebviewclient (noadwebviewclient webclient) to mask the advertised webview.

Noadwebviewclient Shielding Ads

Package cn.wangbaiyuan.webviewadblock;

Import Android.content.Context;
Import Android.util.Log;
Import Android.webkit.WebResourceResponse;
Import Android.webkit.WebView;
Import android.webkit.WebViewClient;

/**
* Created by Brainwang on 05/01/2016.
*/
public class Noadwebviewclient extends Webviewclient {
Private String Homeurl;
private context;

Public noadwebviewclient (Context context,string Homeurl) {
This.context = context;
This.homeurl = Homeurl;
}
@Override
Public Webresourceresponse shouldinterceptrequest (webview view, String URL) {
url = url.tolowercase ();
if (!url.contains (Homeurl)) {
if (! Adfiltertool.hasad (context, url)) {
Return super.shouldinterceptrequest (view, URL);
}else{
return new Webresourceresponse (null,null,null);
}
}else{
Return super.shouldinterceptrequest (view, URL);
}


}
}

Adfiltertool class that determines whether a URL contains ads: This class is determined by whether the URL is contained in an Ad interceptor library

Package cn.wangbaiyuan.webviewadblock;

Import Android.content.Context;
Import android.content.res.Resources;
Import Android.util.Log;

/**
* Created by Brainwang on 05/01/2016.
*/
public class Adfiltertool {
public static Boolean Hasad (context, String URL) {
Resources res = context.getresources ();
string[] Adurls = Res.getstringarray (R.array.adblockurl);
for (String adurl:adurls) {
if (Url.contains (Adurl)) {
return true;
}
}
return false;
}
}

Advertising URL resource file (AD Intercept library can be updated by Baidu): Adurlstring.xml

The so-called advertising interception library, is actually requesting the URL collection of advertising resources, the network has a large number of AD interception library, readers can regularly update the file to achieve the efficient filtering of ads. This article masks the way is more brutal, usually contains the advertising resources of the domain name are prohibited. To achieve more accurate filtering, friends and relatives you can use wildcard matching URL to intercept the way, now the PC-side browser is doing so.

<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<string-array name= "Adblockurl" >
<item>ubmcmm.baidustatic.com</item>
<item>cpro2.baidustatic.com</item>
<item>cpro.baidustatic.com</item>
<item>s.lianmeng.360.cn</item>
<item>nsclick.baidu.com</item>
<item>pos.baidu.com</item>
<item>cbjs.baidu.com</item>
<item>cpro.baidu.com</item>
<item>images.sohu.com/cs/jsfile/js/c.js</item>
<item>union.sogou.com/</item>
<item>sogou.com/</item>
<item>a.baidu.com</item>
<item>c.baidu.com</item>

</string-array>
</resources>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.