Android 获取自带浏览器上网记录

时间:2022-02-06 04:28:49

先是搜索了一下,在manifest里添加

  1. <uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>

有了这个权限就可以读取上网记录和书签了。开始时我以为只有上网记录,但是明显bookmarks是表示书签啊。而书签一般是没有时间这个内容的。所以对query语句进行了修改,添加搜索限制条件。

  1. contentResolver.query(Uri.parse("content://browser/bookmarks"), new String[] {
  2. "title", "url", "date" }, "date!=?",new String[] { "null" }, "date desc");

这句表示在路径“content:……bookmarks”里搜索title, url, date这三列,条件是date!=null,并按照日期降序排序。

其实最开始的时候我是没有添加时间的,但是想想获取上网记录也关心时间,就想添加这个属性,可是发现在三星某款手机里不可以,因为一开始我搜索的时候没有添加限制条件,所以连书签都检索出来了,就像之前说的,书签是不会有时间这个属性的(这应该是一般情况)。而很奇怪的是,之前没有修改的代码在小米上就可以运行,而且只是检索出来上网记录,没有包括书签(这才是特殊情况……)。应该是小米做了修改,啊啊,android的碎片化好头疼啊。

以下是全部代码:

    1. public class GetInternetRecord {
    2. String records = null;
    3. StringBuilder recordBuilder = null;
    4. public void getRecords(ContentResolver contentResolver) {
    5. // ContentResolver contentResolver = getContentResolver();
    6. Cursor cursor = contentResolver.query(
    7. Uri.parse("content://browser/bookmarks"), new String[] {
    8. "title", "url", "date" }, "date!=?",
    9. new String[] { "null" }, "date desc");
    10. while (cursor != null && cursor.moveToNext()) {
    11. String url = null;
    12. String title = null;
    13. String time = null;
    14. String date = null;
    15. recordBuilder = new StringBuilder();
    16. title = cursor.getString(cursor.getColumnIndex("title"));
    17. url = cursor.getString(cursor.getColumnIndex("url"));
    18. date = cursor.getString(cursor.getColumnIndex("date"));
    19. SimpleDateFormat dateFormat = new SimpleDateFormat(
    20. "yyyy-MM-dd hh:mm;ss");
    21. Date d = new Date(Long.parseLong(date));
    22. time = dateFormat.format(d);
    23. System.out.println(title + url + time);
    24. }
    25. }
    26. }