Android, How to get searched keywords from Chrome browser (Search Uri)? - android

I'm able to get searched keywords from default browser by following code.
List<SearchRecord> searchList = new LinkedList<SearchRecord>();
ContentResolver resolver = this.getContentResolver();
Cursor cursor = resolver.query(Browser.SEARCHES_URI, Browser.SEARCHES_PROJECTION, null, null, null);
cursor.moveToFirst();
if (cursor.moveToFirst() && cursor.getCount() > 0) {
while (cursor.isAfterLast() == false) {
SearchRecord record = new SearchRecord();
record.setKeyword(cursor.getString(Browser.SEARCHES_PROJECTION_SEARCH_INDEX));
record.setDate(cursor.getLong(Browser.SEARCHES_PROJECTION_DATE_INDEX));
searchList.add(record);
cursor.moveToNext();
}
}
Following code returns list of bookmarks
Uri uriCustom = Uri.parse("content://com.android.chrome.browser/bookmarks");
I'm looking for Uri of Chrome in order to get searched keywords. Do you have any idea what it is? Thanks

List<SearchRecord> searchList = new LinkedList<SearchRecord>();
ContentResolver resolver = this.getContentResolver();
Cursor cursor = resolver.query(Browser.SEARCHES_URI, Browser.SEARCHES_PROJECTION, ***Browser.BookmarkColumns.BOOKMARK+ " = 0"***, null, null);
cursor.moveToFirst();
if (cursor.moveToFirst() && cursor.getCount() > 0) {
while (cursor.isAfterLast() == false) {
SearchRecord record = new SearchRecord();
record.setKeyword(cursor.getString(Browser.SEARCHES_PROJECTION_SEARCH_INDEX));
record.setDate(cursor.getLong(Browser.SEARCHES_PROJECTION_DATE_INDEX));
searchList.add(record);
cursor.moveToNext();
}
}

Related

How to retrieve and display browser history and bookmarks in browser application [duplicate]

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 :-)

Android - MediaStore.Video.query() is returning null

I'm trying to retrieve the metadata from a video file (title, language, artist) using the method MediaStore.Video.query(). However, the method is always returning null. The code is bellow:
String[] columns = {
MediaStore.Video.VideoColumns._ID,
MediaStore.Video.VideoColumns.TITLE,
MediaStore.Video.VideoColumns.ARTIST
};
Cursor cursor = MediaStore.Video.query(getApplicationContext().getContentResolver(), videoUri,columns);
if (cursor != null) {
cursor.moveToNext();
}
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Video.VideoColumns.TITLE));
Any suggestion about how to return video metadata using android?
==Update
As I searched in many places, I tried one solution using CursorLoader. However, the method loadInBackground() from CursorLoader is also returning null. The code is showed bellow:
String[] columns = {
MediaStore.Video.VideoColumns.TITLE
};
Uri videoUri = Uri.parse("content://mnt/sdcard/Movies/landscapes.mp4");
CursorLoader loader = new CursorLoader(getBaseContext(), videoUri, columns, null, null, null);
Cursor cursor = loader.loadInBackground();
cursor.moveToFirst();
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Video.VideoColumns.TITLE));
Uri.parse("content://mnt/sdcard/Movies/landscapes.mp4") is not an Uri for MediaStore. It would try to find a ContentProvider for authority mnt which does not exist.
MediaStore can handle only content://media/... Uris which you should get exclusively via MediaStore, not by using Uri.parse().
In your case use the following for example
Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String[] columns = {
MediaStore.Video.VideoColumns._ID,
MediaStore.Video.VideoColumns.TITLE,
MediaStore.Video.VideoColumns.ARTIST
};
String selection = MediaStore.Video.VideoColumns.DATA + "=?";
String selectionArgs[] = { "/mnt/sdcard/Movies/landscapes.mp4" };
Cursor cursor = context.getContentResolver().query(uri, columns, selection, selectionArgs, null);
The MediaStore.Video.VideoColumns.DATA field holds the path to the videos and you search for a certain video this way. At least for now, future versions of Android may change that.
Your second example is using CursorLoader the wrong way. If you call loader.loadInBackground() yourself, you load the data in foreground. See e.g. http://mobile.tutsplus.com/tutorials/android/android-sdk_loading-data_cursorloader/
The next thing you do is
Cursor cursor = getCursor();
cursor.moveToFirst();
String title = cursor.getString(/* some index */);
This will lead to a CursorIndexOutOfBoundsException if your cursor has 0 rows and cursor.moveToFirst() failed because there is no first row. The cursor stays before the first row (at -1) and that index does not exist. That would mean in your case that the file was not found in the database.
To prevent that use the return value of moveToFirst - it will only be true if there is a first row.
Cursor cursor = getCursor(); // from somewhere
if (cursor.moveToFirst()) {
String title = cursor.getString(/* some index */);
}
A more complete example including checks for null and closing the cursor in all cases
Cursor cursor = getCursor(); // from somewhere
String title = "not found";
if (cursor != null) {
if (cursor.moveToFirst()) {
title = cursor.getString(/* some index */);
}
cursor.close();
}
I guess the file you try to find is either not indexed in the database (rebooting forces the indexer to run again) or the path is wrong.
Or the path you use is actually a symlink in which case MediaStore might use a different path.
Use this to get rid of symlinks
String path = "/mnt/sdcard/Movies/landscapes.mp4";
try {
path = new File(path).getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
}
Yes, I tested now and it is throwing IndexOutOfBoundsException. When I'm using cursor.getColumnCount() it returns 1
cursor.getColumnCount() is the column count, not the row count. It should always be the same as the number of columns you requested in columns. You need to check cursor.getCount() if you want to check the row count.
Try dumping all the videos known to MediaStore into logcat in case it does not show as expected.
public static void dumpVideos(Context context) {
Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaStore.Video.VideoColumns.DATA };
Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
int vidsCount = 0;
if (c != null) {
vidsCount = c.getCount();
while (c.moveToNext()) {
Log.d("VIDEO", c.getString(0));
}
c.close();
}
Log.d("VIDEO", "Total count of videos: " + vidsCount);
}
I updated your code, it works, just check it
public static void dumpVideos(Context context) {
Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaStore.Video.VideoColumns.DATA };
Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
int vidsCount = 0;
if (c != null) {
c.moveToFirst();
vidsCount = c.getCount();
do {
Log.d("VIDEO", c.getString(0));
}while (c.moveToNext());
c.close();
}
Log.d("VIDEO", "Total count of videos: " + vidsCount);
}

how to get the group id of a contact in android

I want to get the group id of each contact in my android application.I have initial query like this
Cursor cursor = cr.query(Phone.CONTENT_URI,
new String[] {
Phone.CONTACT_ID}, null, null, null);
And what i do is get details of each which related to this CONTACT_ID.I have manage to get phone number,names,addresses and emails so far but still couldn't manage to get the group id of the contact.
P.S i found a question asked before here but the class android.provider.Contacts.GroupMembership is deprecated.
thanks.
Try this:
final String selection = "mimetype_id = (select _id from mimetypes where mimetype = \"" +
vnd.android.cursor.item / group_membership + "\")";
Cursor cursor = getContentResolver().query(Data.CONTENT_URI,
new String[]{Phone.CONTACT_ID}, selection, null, null);
try {
if (cursor != null && cursor.moveToFirst()) {
do {
Log.i("Details", "Contact IDs" + cursor.getLong(cursor.getColumnIndex(Phone.CONTACT_ID)));
} while (cursor.moveToNext());
}
} finally {
if (cursor != null) {
cursor.close();
}
}

Confused on how to query Contacts in Android

I am making a Android application which uses Contacts. The good thing is I managed somehow to make it work with Contacts.Phones as seen on many tutorials. The problem is that Contacts.Phones is deprecated and is replaced by ContactsContract. My application needs to work starting from Android 1.5+.
I need to do some simple operations like:
- query all contacts
- query for a specific contact
- backup all contacts
What is the best way to achieve this, considering I need to have the application working on all versions of android. Do I need to check for current api level on the phone and have 2 code blocks, one before api 5 one after ?
Here is an optional solution
int apiVersion = android.os.Build.VERSION.SDK_INT;
if(apiVersion < 5) {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(People.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(People._ID));
String name = cur.getString(cur.getColumnIndex(People.DISPLAY_NAME));
}
}
} else {
String columns[] = new String[]{ ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME };
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
columns,
null,
null,
ContactsContract.Data.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
long id = Long.parseLong(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)));
String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)).trim();
}
}
}
And here an tutorial to make application Supporting the old and new APIs in the same application this must help you.
Using ContentResolver. Try this code:
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
//Query phone here. Covered next
}
}
}

Android read browser history

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;
}

Categories

Resources