Programmatically finding Chrome browser history

前端 未结 1 743
遥遥无期
遥遥无期 2021-02-01 11:19

I am making a small Java application to show which URLs in Chrome that the user has visited the most. How would I access my browser history within Java?

相关标签:
1条回答
  • 2021-02-01 11:48

    Create a ContentObserver class ...

    static class ChromeOberver extends ContentObserver {   
        public ChromeOberver(Handler handler) { 
            super(handler);          
        } 
    
        @Override
        public void onChange(boolean selfChange) { 
            onChange(selfChange, null); 
        }    
    
        @Override
        public void onChange(boolean selfChange, Uri uri) {
            super.onChange(selfChange);
            Log.d(TAG, "onChange: " + selfChange);
    
            Cursor cursor = context.getContentResolver()
                  .query(CHROME_BOOKMARKS_URI, new String() {"title", "url"}, 
                                               "bookmark = 0", null, null);
    
            // process cursor results
        }
    }
    

    and register that class to monitor history/bookmark changes:

    private static String CHROME_BOOKMARKS_URI = 
           "content://com.android.chrome.browser/bookmarks";
    
    ChromeOberver observer = new ChromeOberver();
    resolver.registerContentObserver(CHROME_BOOKMARKS_URI, true, observer);
    

    Don't forget the permission:

    <uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />
    
    0 讨论(0)
提交回复
热议问题