I'd like to write a small android widget to getting bookmarks from my firefox browser, but my code:
Cursor myCursor=Browser.getAllBookmarks(main.getContentResolver());
not working. This cursor is always empty. It is very strange, because all my browsers (firefox, chrome) have a lot of bookmarks.
I have found this code:
String query = Browser.BookmarkColumns.BOOKMARK+"=1";
Cursor crs=main.getContentResolver().query( UriProvider.QUERY
, columns
, query
, null
, sortOrder
);
but efect is the same, result is empty.
I have problem with understanding nature of android.provider.Browser class. Is it a interface to database table ? What kind of data I can find in this table (bookmarks from firefox, or chrome, or both ???). When this table is synchronized with ff/chrome ?
Thanks for any suggestions...
Best regards
mario
String[] proj = new String[] { Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL };
String sel = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history, 1 =bookmark
mCur = this.managedQuery(Browser.BOOKMARKS_URI, proj, sel, null, null);
this.startManagingCursor(mCur);
mCur.moveToFirst();
String title = "";
String url = "";
if (mCur.moveToFirst() && mCur.getCount() > 0) {
while (mCur.isAfterLast() == false && cont) {
title = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.TITLE));
url = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.URL));
// Do something with title and url
mCur.moveToNext();
}
}
have you added permission in AndroidManifest.xml
<uses-permission
android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>
Thank you for reply.
Your code works but only for default build-in browser.
For chrome I have to use special content provider uri:
Uri uriCustom = Uri.parse("content://com.android.chrome.browser/bookmarks");
For FF the thing is more complicated, because when I try use uri:
Uri uriCustom = Uri.parse("content://org.mozilla.firefox.db.browser/bookmarks");
I get :
java.lang.SecurityException: Permission Denial: reading org.mozilla.firefox.db.BrowserProvider uri content://org.mozilla.firefox.db.browser/bookmarks from pid=xxxx, uid=xxxx requires org.mozilla.firefox.permissions.BROWSER_PROVIDER, or grantUriPermission()
Related
This question already has answers here:
Get browser history and search result in android
(2 answers)
Closed 8 years ago.
Hi friends i am new to android I don't know how to retrieve and display history in web browser.
Please guide me and share your thoughts, ideas and bookmarks too.
I have to display history in ListView
You can use getContentResolver to get the browser history as managedQuery has been depricated.
String[] proj = new String[] { Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL };
String sel = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history, 1 = bookmark
Cursor mCur = getContentResolver().query(Browser.BOOKMARKS_URI, proj, sel, null, null);
mCur.moveToFirst();
String title = "";
String url = "";
if (mCur.moveToFirst() && mCur.getCount() > 0) {
boolean cont = true;
while (mCur.isAfterLast() == false && cont) {
title = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.TITLE));
url = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.URL));
// Do something with title and url
mCur.moveToNext();
}
}
Also add permissions in your manifest
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />
For more details to access data using Cursor check Access Data using Cursor
ArrayList mTitles=new ArrayList();
ArrayList mUrls=new ArrayList();
public void getBrowserHist() {
Cursor mCur = managedQuery(Browser.BOOKMARKS_URI,
Browser.HISTORY_PROJECTION, null, null, null);
mCur.moveToFirst();
if (mCur.moveToFirst() && mCur.getCount() > 0) {
while (mCur.isAfterLast() == false) {
Log.v("titleIdx", mCur
.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX));
Log.v("urlIdx", mCur
.getString(Browser.HISTORY_PROJECTION_URL_INDEX));
mCur.moveToNext();
}
}
}
Add below uses-permission into your manifest file.
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>
Enjoy :-)
can we get chrome browsing history/bookmarks like we get in default browser using READ_HISTORY_BOOKMARKS permission?
PS:I Just want to know is it possible?
Yes it is very much possible.
Use this uri: content://com.android.chrome.browser/bookmarks instead of Browser.BOOKMARKS_URI
String[] proj = new String[] { Browser.BookmarkColumns.TITLE,Browser.BookmarkColumns.URL };
Uri uriCustom = Uri.parse("content://com.android.chrome.browser/bookmarks");
String sel = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history, 1 = bookmark
Cursor mCur = getContentResolver().query(uriCustom, proj, sel, null, null);
mCur.moveToFirst();
#SuppressWarnings("unused")
String title = "";
#SuppressWarnings("unused")
String url = "";
if (mCur.moveToFirst() && mCur.getCount() > 0) {
boolean cont = true;
while (mCur.isAfterLast() == false && cont) {
title = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.TITLE));
url = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.URL));
// Do something with title and url
mCur.moveToNext();
}
}
Havent tested the code for errors but it should work fine. The important thing is knowing the uri to use. Reading this and this might help.
I query the phone's calllog into a ListView. So when the user long clicks an item, a dialog comes up with options, including "View contact". To be able to view the contact the intent needs the contact id.
My problem is that I not always get to see the right contact. I click on Peter, and Peter's contact sheet comes up. I click on Sarah, and Jason's contact sheet comes up.
I must have been using this code the wrong way. Please help.
ContentResolver contentResolver = getContentResolver();
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone));
Cursor cursor = contentResolver.query(uri, new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID}, null, null, null);
if(cursor!=null) {
while(cursor.moveToNext())
{
String contactName = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
contactid2 = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup._ID));
}
cursor.close();
}
Intent intent_contacts = new Intent(Intent.ACTION_VIEW, Uri.parse("content://contacts/people/" + contactid2));
startActivity(intent_contacts);
Maybe what I need is not the PhoneLookup._ID, but some other ID.
I also tried Felipe's answer here, same happens.
Edit:
on a HTC Desire HD (2.3.5) I get the proper contacts in 99% of the
cases.
on a ZTE Blade (2.2) I get the proper contacts in 60% of the cases.
on a Samsung Galaxy Ace (2.3.3) I get the proper contacts in 5% of the cases.
What the hell is going on???
After digging myself into theme, I found sven's help on googlegroups. The problem was that I was using a depracated intent. I read through many pages on the developer site, I must have missed it somehow.
I also post the full code.
contactid26 = null;
ContentResolver contentResolver = getContentResolver();
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phonenumintolist));
Cursor cursor =
contentResolver.query(
uri,
new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID},
null,
null,
null);
if(cursor!=null) {
while(cursor.moveToNext()){
String contactName = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
contactid26 = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup._ID));
}
cursor.close();
}
if (contactid26 == null) {
Toast.makeText(DetailedCallHistory.this, "No contact found associated with this number", Toast.LENGTH_SHORT).show();
}
else {
Intent intent_contacts = new Intent(Intent.ACTION_VIEW, Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactid26)));
//Intent intent_contacts = new Intent(Intent.ACTION_VIEW, Uri.parse("content://contacts/people/" + contactid26));
startActivity(intent_contacts);
}
Here in this case i am going to show you that how to get the ID, and
Name of any contact no, which you have enter and want to search, For
Example if you enter a no and want to search its Name and id then use
this code, it is working 100%, if you want further modification in
this, then you can tell me
Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,Uri.encode(phnno.getText().toString()));//phone no
String[] proj = new String[]
{///as we need only this colum from a table...
Contacts.DISPLAY_NAME,
Contacts._ID,
};
String id=null;
String sortOrder1 = StructuredPostal.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor crsr = getContentResolver().query(lookupUri,proj, null, null, sortOrder1);
while(crsr.moveToNext())
{
String name=crsr.getString(crsr.getColumnIndex(Contacts.DISPLAY_NAME));
id = crsr.getString(crsr.getColumnIndex(Contacts._ID));
Toast.makeText(this,"Name of this cntct is : " +name +"\n id is : "+id ,Toast.LENGTH_SHORT).show();
}
crsr.close();
I can't seem to find any good example code of how to query the UserDictionary content provider given a word. My query looks like:
Cursor cur = getContentResolver().query(
UserDictionary.Words.CONTENT_URI,
new String[] {Words._ID, Words.WORD},
Words.WORD + "=?",
new String[] {"test"},
null);
I have also tried not specifying a query as well as not specifying a projection and the cursor is always empty. I have included android.permission.READ_USER_DICTIONARY in my manifest.
Try this
final String[] QUERY_PROJECTION = {
UserDictionary.Words._ID,
UserDictionary.Words.WORD
};
Cursor cursor = getContentResolver()
.query(UserDictionary.Words.CONTENT_URI, QUERY_PROJECTION, "(locale IS NULL) or (locale=?)",
new String[] { Locale.getDefault().toString() }, null);
I have not tested this, just a suggestion
Precondition: Make sure you have appropriate words in UserDictionary at
Settings ->Language & Input -> Personal dictionary.
Sample sql query which searches for words containing SO in user dictionary and it's equivalent Android code sample. Notice the usage of ? to be replaced by args.
SQL query:
SELECT UserDictionary.Words._ID, UserDictionary.Words.WORD FROM UserDictionary.Words.CONTENT_URI WHERE UserDictionary.Words.WORD LIKE "%SO%
Equivalent Code:
String[] columns = {UserDictionary.Words._ID, UserDictionary.Words.WORD};
String condition = UserDictionary.Words.WORD + " LIKE ? ";
// ? in condition will be replaced by `args` in order.
String[] args = {"%SO%"};
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(UserDictionary.Words.CONTENT_URI, columns, condition, args, null);
//Cursor cursor = resolver.query(UserDictionary.Words.CONTENT_URI, projection, null, null, null); - get all words from dictionary
if ( cursor != null ) {
int index = cursor.getColumnIndex(UserDictionary.Words.WORD);
//iterate over all words found
while (cursor.moveToNext()) {
//gets the value from the column.
String word = cursor.getString(index);
Log.i(TAG, "Word found: " + word);
}
}
Permissions in AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_USER_DICTIONARY"/>
Starting on API 23, the user dictionary is only accessible through IME
and spellchecker.
https://developer.android.com/reference/android/provider/UserDictionary.html
android.permission.READ_USER_DICTIONARY permission is no longer available in M
I want to read browser history in Android phone.
I have done some document reading, then I come to know that we can read browser history by android.provider.Browser class. It has :
final static Cursor
getAllVisitedUrls(ContentResolver cr)
...method which returns Cursor.
May I get help to handle Cursor, or any example code to get browser history?
Not really an answer but I can tell you what I did.
I first clone the browser repo and try to reproduce how they get the history.
And I started getting:
Permission Denial: reading
com.android.browser.BrowserProvider
So I added:
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />
But it still is giving me the same error. I google it and I found this Accessing Data With Android Cursors.
Hope it helps.
managedQuery has been deprecated so use getContentResolver instead, use the following code:
String[] proj = new String[] { Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL };
String sel = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history, 1 = bookmark
Cursor mCur = getContentResolver().query(Browser.BOOKMARKS_URI, proj, sel, null, null);
mCur.moveToFirst();
#SuppressWarnings("unused")
String title = "";
#SuppressWarnings("unused")
String url = "";
if (mCur.moveToFirst() && mCur.getCount() > 0) {
boolean cont = true;
while (mCur.isAfterLast() == false && cont) {
title = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.TITLE));
url = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.URL));
// Do something with title and url
mCur.moveToNext();
}
}
Also add permissions using
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />
For Lollipop or earlier
I am able to get the history by using the following code:
Cursor mCur = activity.managedQuery(Browser.BOOKMARKS_URI,
Browser.HISTORY_PROJECTION, null, null, null);
if (mCur.moveToFirst()) {
while (mCur.isAfterLast() == false) {
Log.v("titleIdx", mCur
.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX));
Log.v("urlIdx", mCur
.getString(Browser.HISTORY_PROJECTION_URL_INDEX));
mCur.moveToNext();
}
}
This post is a little bit old, but here is another easy solution for getting data related to Bookmark and Search content providers in Android:
Use this lib: https://github.com/EverythingMe/easy-content-providers
Get all bookmarks:
BrowserProvider browserProvider = new BrowserProvider(context);
List<Bookmark> bookmarks = browserProvider.getBookmarks().getList();
Each Bookmark has all fields, so you can get any info you need:
title, url, visits, ...
Get all Search history:
List<Search> searches = browserProvider.getSearches().getList();
It works with lists or cursor and there a sample app to see how it looks and works.
In fact, there is support for all Android content providers like: Contacts, SMS, Calls, ...
Full doc with all options: https://github.com/EverythingMe/easy-content-providers/wiki/Android-providers
Hope it helped :)
public ArrayList<HistoryEntry> getBrowserHistory() {
String title = "";
String url = "";
ArrayList<HistoryEntry> list = new ArrayList<HistoryEntry>();
String[] proj = new String[] { Browser.BookmarkColumns.TITLE,
Browser.BookmarkColumns.URL };
String sel = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history,
// 1 = bookmark
Cursor mCur = getContentResolver().query(Browser.BOOKMARKS_URI, proj,
sel, null, null);
mCur.moveToFirst();
if (mCur.moveToFirst() && mCur.getCount() > 0) {
boolean cont = true;
while (mCur.isAfterLast() == false && cont) {
HistoryEntry entry = new HistoryEntry();
title = mCur.getString(mCur
.getColumnIndex(Browser.BookmarkColumns.TITLE));
url = mCur.getString(mCur
.getColumnIndex(Browser.BookmarkColumns.URL));
// Do something with title and url
entry.setTitle(title);
entry.setUrl(url);
list.add(entry );
Log.d("TAG", "title " + title);
mCur.moveToNext();
}
}
mCur.close();
return list;
}