2013-09-24 256 views
1

我有一個代碼可以在android中檢索默認瀏覽器的歷史記錄,最近我添加了Chrome瀏覽器歷史記錄。但是我有一個問題 - 我只有鉻歷史。例如。如果沒有chrome,我會得到默認的瀏覽器,如果有chrome,這兩種方法都只能獲得chrome的歷史記錄。 我使用此代碼 -獲取Android中的默認瀏覽器和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"))); 
    }} 

我可以得到默認瀏覽器,然後鉻?我確實嘗試將「Browser.BOOKMARKS_URI」更改爲uri-「content://com.android.browser/bookmarks」,但它沒有奏效。

有什麼建議嗎?

回答

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)。如果您想讓Google Chrome工作,只需將Browser.BOOKMARKS_URI替換爲Uri.parse(「content://com.android.chrome.browser/bookmarks」)

注意,你將不得不更換其兩個getContentResolver()。registerContentObserver(...)getContentResolver()查詢(...)