查询Android浏览器历史记录以获取URL

时间:2013-04-10 15:02:40

标签: java android google-chrome browser android-intent

我可以使用以下方法查看Android浏览器历史记录:

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Browser;
import android.widget.TextView;

public class ReadHistoryDetails extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.hello);
String[] projection = new String[] { Browser.BookmarkColumns.TITLE,
Browser.BookmarkColumns.URL };
Cursor mCur = managedQuery(android.provider.Browser.BOOKMARKS_URI,
projection, null, null, null);
mCur.moveToFirst();
int titleIdx = mCur.getColumnIndex(Browser.BookmarkColumns.TITLE);
int urlIdx = mCur.getColumnIndex(Browser.BookmarkColumns.URL);
while (mCur.isAfterLast() == false) {
tv.append("\n" + mCur.getString(titleIdx));
tv.append("\n" + mCur.getString(urlIdx));
mCur.moveToNext();
}
} 

但是,如果搜索符合我的预定义标准之一,我想搜索浏览器历史记录并执行新意图。

假设我想查询包含playboy.com的任何内容的浏览器历史记录

我认为我会使用以下内容:

public class ReadHistoryDetails extends Activity {
{
TextView tv = (TextView)findViewById(R.id.hello);
String[] projection = new String[] { Browser.BookmarkColumns.TITLE,
Browser.BookmarkColumns.URL };

Cursor cursor = managedQuery(android.provider.Browser.BOOKMARKS_URI,
projection, null, null, null);
 String urls = "";
if (cursor.moveToFirst()) {
String url1 = null;
String url2 = null;
do {
urls = urls
+ cursor.getString(cursor.getColumnIndex(Browser.BookmarkColumns.TITLE)) + " : "
+ cursor.getString(cursor.getColumnIndex(Browser.BookmarkColumns.URL)) + "\n";
} while (cursor.moveToNext()); 
tv.setText(urls);
}

但是我收到一条错误,指出setText(String) is undefined for the type String ....,以及由于格式错误的代码导致搜索结果不足。

1 个答案:

答案 0 :(得分:1)

天真的实施:

Cursor cursor = managedQuery(android.provider.Browser.BOOKMARKS_URI,
                             projection, null, null, null);
String urls = "";
if (cursor.moveToFirst()) {
    String url1 = null;
    String url2 = null;
    do {
        // Just check if the url contains www.playboy.com
        String url = cursor.getString(cursor.getColumnIndex(Browser.BookmarkColumns.URL));
        if (url.toLowerCase().contains("www.playboy.com")) {
        urls = urls
             + cursor.getString(cursor.getColumnIndex(Browser.BookmarkColumns.TITLE)) + " : "
             + url + "\n";
        }
    } while (cursor.moveToNext()); 
tv.setText(urls);
}

更优化的实现是将过滤器直接包含在查询中:

Cursor cursor = managedQuery(android.provider.Browser.BOOKMARKS_URI,
                             projection, Browser.BookmarkColumns.URL + " LIKE '%playboy.com%'" , null, null);