1

我有一个代码可以检索 android 中默认浏览器的历史记录,最近我添加了 chrome 浏览器历史记录。但我有一个问题 - 我只得到 chrome 历史记录。例如,如果没有 chrome,我会得到默认浏览器,如果有 chrome,这两种方法都会获取只有 chrome 的历史记录。我使用这个代码 -

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")));   
    }}

我可以先获得默认浏览器,然后再获得 chrome 吗?我确实尝试将“Browser.BOOKMARKS_URI”更改为 uri-“content://com.android.browser/bookmarks”,但没有成功。

有什么建议么?

4

1 回答 1

0

我想您正在使用 ContentObserver。类似于以下内容:

//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();
    }
}

然后您使用以下方式注册/注销它:

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

...

getContentResolver().unregisterContentObserver(browserHistoryObserver);

当然,上面的代码适用于默认的 Android 浏览器(实际上我一直使用Browser.BOOKMARKS_URI)。如果你想让它适用于谷歌浏览器,你只需要将Browser.BOOKMARKS_URI替换为Uri.parse("content://com.android.chrome.browser/bookmarks")

请注意,您必须在getContentResolver().registerContentObserver(...)getContentResolver().query(...)中替换它。

于 2013-10-24T13:13:20.207 回答