where clause in contentProvider's query in Android - android

I want to add functionality of contains() method in where clause. Right now I was doing something like "address = ?" and providing address in arguments but now I want to use a logic which works as address.contains(?) and then providing the address in arguments. How can I implement it?
Till now my code is and need to know the require modification.
Uri mSmsinboxQueryUri = Uri.parse("content://sms");
String[] projection = new String[] { "_id", "thread_id", "address",
"person", "date", "body", "type" };
Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,
projection, "address = ?", new String[]{addressToBeSearched}, null);

Try this
Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,
projection, "address LIKE ?", new String[]{addressToBeSearched + "%" }, null);

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.

Search Phone numbers from Android Contact Database

I am implementing an AutocompleteView to search phone numbers. The code is working fine except in some conditions.
My Code :
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[]{ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE};
String selection = ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE ?";
String[] selectionArgs = new String[]{"%" + charSequence.toString() + "%"};
Cursor cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
This code is working fine where there is no space in the phone numbers. For example if i enter '123' in my autocompleteView, it is able to find phone numbers like '9123456789' or '8283929383' but it is not able to find numbers '9123 456 789' or '912 3456 789'
I even tried implementing this with ContactsContract.PhoneLookup API but with this, it didnt work at all.
Code with ContactsContract.PhoneLookup API:
String[] projection = new String[]{ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup.NUMBER, ContactsContract.PhoneLookup.TYPE};
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(charSequence.toString()));
Cursor cursor = context.getContentResolver().query(uri, projection, null, null, ContactsContract.PhoneLookup.DISPLAY_NAME + " ASC");
Any help is appreciated.
Instead of using ContactsContract.CommonDataKinds.Phone.NUMBER, use ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER (this return phone numbers in E164 format)

How to get only sent messages from specific conversation in Android Studio?

ContentResolver cr = getContentResolver();
String[] projection = new String[]{"body", "ct_t", "_id", "address"};
Uri uri = Uri.parse("content://mms-sms/conversations/" + id);
Cursor c = cr.query(uri, projection, null, null, null);
This queries all messages from a specific conversation and I would like only the sent messages to display as I need to distinguish each party of the conversation.
Is there such a thing as a uri like this:
Uri uri2 = Uri.parse("content://mms-sms/conversations/" + id + "/sent/");
You'll need to add a couple of columns to your projection, and include a selection argument in the query() call.
final String[] projection = {"_id", "address", "body", "ct_t", "type", "msg_box"};
final String selection = "(type = 2 OR msg_box = 2)";
final Uri uri = Uri.parse("content://mms-sms/conversations/" + threadId);
Cursor c = getContentResolver().query(uri, projection, selection, null, null);
An SMS message will have a type value of 2 when it has been sent. An MMS message will have the same value, but in the msg_box column.

Use Distnict with getContentREsolver android Sqlite

I am creating a messaging application in android and i want to get text messages from unique contact numbers only.
I tried following code but it is not working.
String[] projection = new String[] { "_id","DISTINCT "+ContactsContract.PhoneLookup.NUMBER, "person", "body", "date", "type" };
Cursor cursor = getContentResolver().query(uri, projection,null, null, null);
It throwing error here.
Caused by: android.database.sqlite.SQLiteException: near "DISTINCT": syntax error: , while compiling: SELECT _id, DISTINCT number, person, body, date, type FROM sms ORDER BY date DESC
Can Some one please crack this out for me.
You need to change your query. The DISTINCT keyword only works with all rows. What you need is a Group By section in your query.
You could change your query to:
String[] projection = new String[] { "_id", ContactsContract.PhoneLookup.NUMBER, "person", "body", "date", "type" };
String selection = GROUP BY " + ContactsContract.PhoneLookup.NUMBER;
Cursor cursor = getContentResolver().query(uri, projection, selection, null, null);
I've not tested this yet, so there might be syntax errors.

Querying MediaStore.Images.Media , how do I query both on EXTERNAL_CONTENT_URI and INTERNAL_CONTENT_URI?

And first of all thanks for your help.
I think this should be a rather trivial question for somebody not new to querying Content Providers.
I need to query MediaStore.Images.Media to obtain ALL the images on the device, both on internal storage and on an sd card.
This is the query I have in mind:
String[] proj = { MediaStore.Images.Media.DATA };
actualimagecursor =
managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj,null, null, null);
the point is that I want to query both EXTERNAL_CONTENT_URI and INTERNAL_CONTENT_URI.
Is it possible to perform it with a single query?
Sorry this is too old to help, but you can use MergeCursor to combine the two queries.
Cursor[] cursors = new Cursor[2];
cursors[0] = mActivity.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[]{
MediaStore.Images.Media._ID,
MediaStore.Images.Media.DATA,
MediaStore.Images.Media.ORIENTATION,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
MediaStore.Images.Media.BUCKET_ID,
MediaStore.Images.Media.MIME_TYPE ,
},
null,
null,
MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC"
);
cursors[1] = mActivity.getContentResolver().query(
MediaStore.Images.Media.INTERNAL_CONTENT_URI,
new String[]{
MediaStore.Images.Media._ID,
MediaStore.Images.Media.DATA,
MediaStore.Images.Media.ORIENTATION,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
MediaStore.Images.Media.BUCKET_ID,
MediaStore.Images.Media.MIME_TYPE
},
null,
null,
MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC"
);
Cursor cursor = new MergeCursor(cursors);
It's not possible to retrieve both results with a single query since the query is permofmed in a specific location (internal or external). You need yo instantiate two dirrefent Cursors.

Categories

Resources