How to add where clause while querying for mms-sms conversation? - android

I need a conversation cursor which does not have given addresses.
This is how i am getting the cursor :
final Uri inboxURI = Uri.parse("content://mms-sms/conversations/");
final String[] projection = new String[] { Sms._ID, Sms.THREAD_ID, Sms.ADDRESS, Sms.BODY, Sms.DATE, Sms.READ};
Cursor cursor = cr.query(inboxURI, projection, Sms.ADDRESS+" NOT IN (?)", new String[]{"456454"}, Telephony.Sms.Inbox.DEFAULT_SORT_ORDER);
This gives me an empty cursor. However this works fine if null is given as selection and selection_args.
Is this correct way?
Thanks in advance.

Related

How to get the Contact Names of Array of ContactNumbers from Contacts in Android?

I have a requirement when I need to get the Contact name of the selected numbers from the ANDROID_CONTACTS.
I can get the name of a particular number using ->
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor cursor = cr.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
if (cursor == null) {
return;
}
How can I pass a list of numbers and get their respective names?
Here's the naive approach:
String[] phones = new String[] { "(212) 555-1111", "(212) 555-2222", "(212) 555-3333"};
String selection = Phone.NUMBER + "IN (?,?,?)";
String[] selectionArgs = phones;
String[] projection = new String[] { Phone.NUMBER, Phone.CONTACT_ID, Phone.DISPLAY_NAME };
Cursor cur = getContentResolver().query(Phone.CONTENT_URI, projection, selection, selectionArgs, null);
DatabaseUtils.dumpCursor(cur); // dumps the cursor to logcat
However, this will only work if you enter the phone number in exactly the same format as it is stored in the ContactsContract DB under NUMBER.
The nice thing about the PhoneLookup.CONTENT_FILTER_URI API you've used is that the format of the number doesn't matter, it'll find it in the DB in any format you use.
To replicate something like that, you can try using another field called NORMALIZED_NUMBER which should always hold the number in a specific format called E164.
So you can run a query like this:
String[] e164_phones = new String[] { "+12125551111", "+12125552222", "+12125553333"};
String selection = Phone.NUMBER + "IN (?,?,?)";
String[] selectionArgs = e164_phones; // all phones here must be in e164 format
String[] projection = new String[] { Phone.NUMBER, Phone.CONTACT_ID, Phone.DISPLAY_NAME };
Cursor cur = getContentResolver().query(Phone.CONTENT_URI, projection, selection, selectionArgs, null);
You'll probably need a way to convert a phone number to E164 format for this code, check out this answer then.

Empty cursor row when filtering with selection

I'm not confident with cursors and I'm facing some problem when filtering one with a WHERE clause.
What I'm doing:
ContentResolver contentResolver = context.getContentResolver();
Uri uriConversation = Uri.parse("content://mms-sms/conversations/");
String[] projection = new String[]{"*"};
String selection = "address=" + phoneNumberForThread;
Cursor cursor = contentResolver.query(uriConversation, projection, null, null, null);
Executing this code the cursor get filled and works perfectly.
However, if I swap the null selection argument with my selection String as
Cursor cursor = contentResolver.query(uriConversation, projection, selection, null, null);
I then get an empty cursor. I even check for !phoneNumberForThread.isEmpty()
I think I'm doing something wrong but again I am not confident with cursor yet.
Any help would be really appreciated.
If your input variabel is stored as a string data type, you should enclose it with ' as such:
String selection = "address='" + phoneNumberForThread + "'";

Android: query for albums gives duplicates

I'm querying the contentresolver in order to list all the albums on the device. However I have losts of duplicate. Is it a problem of my query or of the android database?
Maybe is it because I'm getting albums from a songs' table?
String[] projection = {
MediaStore.Audio.Albums.ALBUM_ID,
MediaStore.Audio.Albums.ALBUM,
MediaStore.Audio.Albums.ARTIST
};
String selection = null;
Cursor cursor = context.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
null,
null);
Thank you

Query with multiple uri's

Uri contactUri1 = Uri.withAppendedPath(
ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number1));
Cursor phones = getContentResolver()
.query(contactUri1, PEOPLE_PROJECTION, null, null, null);
This works. I can get what I need in the cursor given number1.
What I want is to query for multiple numbers and get all the results in the same cursor.
EXAMPLE:
Uri contactUri1 = ...
Uri contactUri2 = ...
Cursor phones = getContentResolver().query(... //Both URI's
Is that possible?

How do I get the _count in my content provider?

What should I do to get my content provider to return the _count column with the count of records? The documentation says it is automatic, but maybe it's only taking about some built-in content provider. Running a query to the database seems not to return it.
If you are using contentProvider then you have to do it like count(*) AS count.
If you use cursor.getCount(), that would not be as efficient as the above approach. With cursor.getCount() you are fetching all the records just to get counts. The entire code should look like following -
Cursor countCursor = getContentResolver().query(CONTENT_URI,
new String[] {"count(*) AS count"},
null,
null,
null);
countCursor.moveToFirst();
int count = countCursor.getInt(0);
The reason why this works is because android needs a column name to be defined.
If you are using ContentProvider.query() a Cursor is returned. Call Cursor.getCount() to get a count of records in the returned cursor.
I had a similiar problem and found this worked for me. In the example below I wanted to get the count of images from the MediaStore provider.
final String[] imageCountProjection = new String[] {
"count(" + MediaStore.Images.ImageColumns._ID + ")",
};
Cursor countCursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
imageCountProjection,
null,
null,
null);
countCursor.moveToFirst();
int existingImageCount = countCursor.getInt(0);
With cursor.getCount() you can not assure that it returns the real number of items returned. There are much better ways:
1- If you are using Content Providers, you can do a query and use the Column (_COUNT) included in BaseColumns for your projection
#Override
public Cursor query(SQLiteDatabase db, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
...
projection = new String[] {
ContentContract.NotificationCursor.NotificationColumns._COUNT,
};
...
Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, groupBy, having, sortOrder);
return cursor;
}
2- To do a rawQuery using SELECT COUNT(*) as #saurabh says in his response.

Categories

Resources