gpt4 book ai didi

Android 监听浏览器历史最后一页

转载 作者:行者123 更新时间:2023-11-30 02:12:48 24 4
gpt4 key购买 nike

我得到了浏览器的历史记录,但只想得到访问过的页面的信息,下面是我的代码,同样我想听到新添加的电话簿联系人

public class HistoryObserver extends ContentObserver {

public final String TAG = "HistoryObserver";

public HistoryObserver(Handler handler) {
super(handler);
Log.d(TAG, "Creating new HistoryObserver");``
}

public HistoryObserver() {
super(null);
Log.d(TAG, "Creating a new HistoryObserver without a Handler");
}

@Override
public boolean deliverSelfNotifications() {
Log.d(TAG, "delivering self notifications");
return true;
}

@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.d(TAG, "onChange without uri: " + selfChange);
//onChange(selfChange, null);
}

@SuppressLint("NewApi") public void onChange(boolean selfChange, Uri uri) {
super.onChange(selfChange, uri);
Log.d(TAG, "onChange: " + selfChange + "\t " + uri.toString());

String[] proj = new String[] { Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL,Browser.BookmarkColumns.DATE };
String selection = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history, 1 = bookmark
Cursor mCursor = getContentResolver().query(Browser.BOOKMARKS_URI, proj, selection, null, null);
//this.startManagingCursor(mCursor);
mCursor.moveToFirst();
int count= mCursor.getColumnCount();
String COUNT=String.valueOf(count);
Log.e("Browser sayac", COUNT);

String title = "";
String url = "";
String date="";
if (mCursor.moveToFirst() && mCursor.getCount() > 0) {

while (mCursor.isAfterLast() == false) {

title = mCursor.getString(mCursor.getColumnIndex(Browser.BookmarkColumns.TITLE ));
url = mCursor.getString(mCursor.getColumnIndex(Browser.BookmarkColumns.URL));
date = mCursor.getString(mCursor.getColumnIndex(Browser.BookmarkColumns.DATE));
Long timestamp = Long.parseLong(date);

SimpleDateFormat dateFormat = new SimpleDateFormat( "dd/MM/yyyy/HH:mm");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timestamp);
String finaldate = dateFormat.format(calendar.getTime());
String smsDate = finaldate.toString();

// Log.e("DTE", date);
Log.e("URL",title );

Log.e("TARIH", smsDate);
mCursor.moveToNext();
}
}
}
}

最佳答案

由于您使用的是 ContentObserver,我猜您想要的是获取上次访问的页面。该过程几乎相同,只是您只需使用 moveToLast() 方法将光标指向最后一个位置。

 @SuppressLint("NewApi") public void onChange(boolean selfChange, Uri uri) {
super.onChange(selfChange, uri);
Log.d(TAG, "onChange: " + selfChange + "\t " + uri.toString());

String[] proj = new String[] { Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL,Browser.BookmarkColumns.DATE };
String selection = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history, 1 = bookmark
Cursor mCursor = getContentResolver().query(Browser.BOOKMARKS_URI, proj, selection, null, null);
//this.startManagingCursor(mCursor);
mCursor.moveToLast();
int count= mCursor.getColumnCount();
String COUNT=String.valueOf(count);
Log.e("Browser sayac", COUNT);

String title = mCursor.getString(mCursor.getColumnIndex(Browser.BookmarkColumns.TITLE ));
String url = mCursor.getString(mCursor.getColumnIndex(Browser.BookmarkColumns.URL));
String date = mCursor.getString(mCursor.getColumnIndex(Browser.BookmarkColumns.DATE));
Long timestamp = Long.parseLong(date);

SimpleDateFormat dateFormat = new SimpleDateFormat( "dd/MM/yyyy/HH:mm");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timestamp);
String finaldate = dateFormat.format(calendar.getTime());
String smsDate = finaldate.toString();

// Log.e("DTE", date);
Log.e("URL",title );

Log.e("TARIH", smsDate);



}

当然,您需要注册您的 ContentObserver 才能使其正常工作。我建议您使用后台服务来注册观察者。

       private BrowserObserver browserObserver;
browserObserver = new BrowserObserver(new Handler(), getBaseContext());
getApplicationContext().getContentResolver().registerContentObserver(Browser.BOOKMARKS_URI, true, browserObserver);

并且不要忘记在您的 list 中添加此权限

关于Android 监听浏览器历史最后一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29877824/

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