Developing a Web Monitor in Android

2019-03-21 14:55发布

问题:

I would like to monitor/filter the websites that an user opens in Android.

I know how to retrieve the last visited URL (in Android default browser) using a ContentObserver on the browser history...

private static class BrowserObserver extends ContentObserver {
    private static String lastVisitedURL = "";
    private static String lastVisitedWebsite = "";

    //Query values:
    final String[] projection = new String[] { Browser.BookmarkColumns.URL };  // URLs
    final String selection = Browser.BookmarkColumns.BOOKMARK + " = 0";  // history item
    final String sortOrder = Browser.BookmarkColumns.DATE;  // the date the item was last visited


    public BrowserObserver(Handler handler) {
        super(handler);
    }


    @Override
    public void onChange(boolean selfChange) {
        onChange(selfChange, null);
    }


    @Override
    public void onChange(boolean selfChange, Uri uri) {
        super.onChange(selfChange);

        //Retrieve all the visited URLs:
        final Cursor cursor = getContentResolver().query(Browser.BOOKMARKS_URI, projection, selection, null, sortOrder);

        //Retrieve the last URL:
        cursor.moveToLast();
        final String url = cursor.getString(cursor.getColumnIndex(projection[0]));

        //Close the cursor:
        cursor.close();

        if ( !url.equals(lastVisitedURL) ) {  // to avoid information retrieval and/or refreshing...
            lastVisitedURL = url;

            //Debug:
            Log.d(TAG, "URL Visited: " + url + "\n");
        }
    }
}

To register the ContentObserver I use:

browserObserver = new BrowserObserver(new Handler());
getContentResolver().registerContentObserver(Browser.BOOKMARKS_URI, true, browserObserver);

And to unregister it:

getContentResolver().unregisterContentObserver(browserObserver);

This works. However, in this way, I can analyze the URLs only after the browser has loaded them.

Now, is there a way to retrieve the URLs before the browser actually loads them in Android?

回答1:

A solution which could help to create a Web Monitor, is to creation your own VPN service, so that you monitor all the device traffic. A good example of this is the project NetGuard.

https://github.com/M66B/NetGuard

Note that in some devices, the system will not pass through the VPN some applications (ex, in Samsung devices, the Samsung Web Browser is not forwarded through the system VPN, checked in S5 with Android 6.0).

Also your application should request the permission to be used as a VPN Service, but once the user gives this permission, it can monitor and filter most of the device network traffic.