问题描述
我有一个code表示检索的Android默认浏览器的历史记录,最近我加入了Chrome浏览器的历史。但我有一个problem-我只得到了镀铬的历史。例如。如果不存在铬,我得到的默认浏览器,如果有铬,这两种方法得到的铬唯一的历史。我用这个code-
I have a code that retrieve the history of the default browser in android, lately I added the chrome browser history. But I have a problem- I get only the chrome history. E.g. if there is no chrome, I get the default browser, if there is chrome, both methods get the chrome only history.I use this code-
String[] mProjection = {
Browser.BookmarkColumns.BOOKMARK,
BookmarkColumns.CREATED, BookmarkColumns.DATE,
BookmarkColumns.TITLE, BookmarkColumns.URL,
BookmarkColumns.VISITS,
BookmarkColumns._ID };
String mSelectionClause = "DATE > ? ";
String[] selectionArgs = {getEpochDate("DefaultBrowser")}; //"1332115200-000"
String mSortOrder = "DATE";
Cursor cr = getContentResolver().query(Browser.BOOKMARKS_URI, mProjection, mSelectionClause, selectionArgs, mSortOrder);
cr.moveToFirst();
if (cr.moveToFirst() && cr.getCount() > 0) {
while (cr.isLast() == false) {
try {
if (cr.getString(cr.getColumnIndex("BOOKMARK")).equals("0")) {
Log.d("getHistory", cr.getString(cr.getColumnIndex("TITLE")));
mc.writeDataToDB("URL", mc.ConvertFromEpoch(cr.getString(cr.getColumnIndex("DATE"))), cr.getString(cr.getColumnIndex("URL")), cr.getString(cr.getColumnIndex("TITLE")),"");
}
} catch (Exception e) {
e.printStackTrace();
}
cr.moveToNext();
}
mc.insertPref("HistoryDate", cr.getString(cr.getColumnIndex("DATE")));
}
// get google chrome history
String[] selectionArgsChrome = {getEpochDate("ChromeBrowser")};
Uri uriCustom = Uri.parse("content://com.android.chrome.browser/bookmarks");
if (getContentResolver().query(uriCustom, mProjection, mSelectionClause, selectionArgsChrome, mSortOrder) !=null){
Cursor mCur = getContentResolver().query(uriCustom, mProjection, mSelectionClause, selectionArgsChrome, mSortOrder);
mCur.moveToFirst();
String title = "";
String url = "";
String DATE = "";
if (mCur.moveToFirst() && mCur.getCount() > 0) {
boolean cont = true;
while (mCur.isAfterLast() == false && cont) {
title = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.TITLE));
url = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.URL));
DATE = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.DATE));
mc.writeDataToDB("URL", mc.ConvertFromEpoch(DATE), url, title,"");
mCur.moveToNext();
}
mc.insertPref("ChromeHistoryDate", cr.getString(cr.getColumnIndex("DATE")));
}}
我可以得到默认的浏览器,然后铬?我曾尝试改变Browser.BOOKMARKS_URI到URI的。内容://com.android.browser/bookmarks,但它并没有奏效。
can I get the default browser and then the chrome? I did try to change the "Browser.BOOKMARKS_URI" to uri- "content://com.android.browser/bookmarks", but it didn't worked.
有什么建议?
推荐答案
我想您使用的是ContentObserver。类似如下:
I suppose you're using a ContentObserver. Something like the following:
//Query values:
static final String[] projection = new String[] { BookmarkColumns.URL, BookmarkColumns.TITLE, BookmarkColumns.VISITS, BookmarkColumns.DATE };
static final String selection = Browser.BookmarkColumns.BOOKMARK + " = 0";
static final String sortOrder = Browser.BookmarkColumns.DATE;
private static class BrowserHistoryObserver extends ContentObserver {
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);
cursor.moveToFirst();
while ( !cursor.isAfterLast() ) {
final String url = cursor.getString(cursor.getColumnIndex(projection[0]));
final String title = cursor.getString(cursor.getColumnIndex(projection[1]));
//visits = ... (cursor.getColumnIndex(projection[2]))
//date = ... (cursor.getColumnIndex(projection[3]))
Log.d(TAG, title + " : " + url + "\n");
cursor.moveToNext();
}
//Close the cursor:
cursor.close();
}
}
和您注册/喜欢的东西注销它:
And you register/unregister it with something like:
browserHistoryObserver = new BrowserHistoryObserver(new Handler());
getContentResolver().registerContentObserver(Browser.BOOKMARKS_URI, true, browserObserver);
...
getContentResolver().unregisterContentObserver(browserHistoryObserver);
当然,code以上,适用于默认的Android浏览器(事实上,我一直使用的 Browser.BOOKMARKS_URI )。如果你想让它工作的谷歌浏览器,你只需要更换 Browser.BOOKMARKS_URI 是的 Uri.parse(内容://com.android.chrome.browser/书签)的
The code above, of course, works for the default Android browser (indeed I've always used Browser.BOOKMARKS_URI). If you want to make it working for Google Chrome, you just need to replace Browser.BOOKMARKS_URI with Uri.parse("content://com.android.chrome.browser/bookmarks").
请注意,你必须把它替换这两个的 getContentResolver()。registerContentObserver(...)和 getContentResolver()查询(...)的
Note that you will have to replace it in both getContentResolver().registerContentObserver(...) and getContentResolver().query(...).
这篇关于获得在Android的默认和Chrome浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!