我可以使用以下方法查看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 ....
时出现错误,并且缺少搜索结果。 最佳答案
天真的实现:
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);