简体   繁体   English

在Android中开发Web监视器

[英]Developing a Web Monitor in Android

I would like to monitor/filter the websites that an user opens in Android. 我想监控/过滤用户在Android中打开的网站。

I know how to retrieve the last visited URL (in Android default browser) using a ContentObserver on the browser history... 我知道如何使用浏览器历史记录中的ContentObserver检索上次访问过的URL(在Android默认浏览器中)...

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: 要注册ContentObserver,我使用:

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. 但是,通过这种方式,我只能在浏览器加载后才能分析URL。

Now, is there a way to retrieve the URLs before the browser actually loads them in Android? 现在,有没有办法在浏览器实际在Android中加载URL之前检索URL?

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. 可以帮助创建Web监视器的解决方案是创建自己的VPN服务,以便监视所有设备流量。 A good example of this is the project NetGuard. 一个很好的例子就是NetGuard项目。

https://github.com/M66B/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). 请注意,在某些设备中,系统不会通过VPN某些应用程序(例如,在三星设备中,三星Web浏览器不通过系统VPN转发,在S5中使用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. 此外,您的应用程序应该请求获得用作VPN服务的权限,但是一旦用户授予此权限,它就可以监视和过滤大多数设备网络流量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM