gpt4 book ai didi

java - 查询 Android 浏览器历史记录中的 URL

转载 作者:行者123 更新时间:2023-12-01 14:41:14 26 4
gpt4 key购买 nike

我可以使用以下方法查看 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();
}
}

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

假设我想查询浏览器历史记录以查找包含 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) 对于 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);

关于java - 查询 Android 浏览器历史记录中的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15929537/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com