我想监控/过滤用户在 Android 中打开的网站。
我知道如何使用浏览器历史记录中的 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");
}
}
}
要注册我使用的 ContentObserver:
browserObserver = new BrowserObserver(new Handler());
getContentResolver().registerContentObserver(Browser.BOOKMARKS_URI, true, browserObserver);
并取消注册:
getContentResolver().unregisterContentObserver(browserObserver);
这行得通。但是,通过这种方式,我只能在浏览器加载 URL 后分析它们。
现在,有没有办法在浏览器实际在 Android 中加载 URL 之前检索它们?