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

先是搜索了一下,在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.   
    5.     public void getRecords(ContentResolver contentResolver) {  
    6.         // ContentResolver contentResolver = getContentResolver();  
    7.         Cursor cursor = contentResolver.query(  
    8.                 Uri.parse("content://browser/bookmarks"), new String[] {  
    9.                         "title""url""date" }, "date!=?",  
    10.                 new String[] { "null" }, "date desc");  
    11.         while (cursor != null && cursor.moveToNext()) {  
    12.             String url = null;  
    13.             String title = null;  
    14.             String time = null;  
    15.             String date = null;  
    16.   
    17.             recordBuilder = new StringBuilder();  
    18.             title = cursor.getString(cursor.getColumnIndex("title"));  
    19.             url = cursor.getString(cursor.getColumnIndex("url"));  
    20.   
    21.             date = cursor.getString(cursor.getColumnIndex("date"));  
    22.   
    23.             SimpleDateFormat dateFormat = new SimpleDateFormat(  
    24.                     "yyyy-MM-dd hh:mm;ss");  
    25.             Date d = new Date(Long.parseLong(date));  
    26.             time = dateFormat.format(d);  
    27.   
    28.             System.out.println(title + url + time);  
    29.         }  
    30.     }  
    31. }  
posted @ 2013-11-13 19:55  安卓吧  阅读(9474)  评论(0编辑  收藏  举报