Have an android app that prints with a toast pop up, and reads out a received message with tts. I use "String origin = smsMessage[0].getOriginatingAddress();" to get the phone number of the sender.
I want to query the contacts list on the phone, so if the received number matches any contacts, it will print & read out the name of the sender instead. Otherwise, if the number is not recognised, it will default back to just printing & reading the OriginatingAddress number.
Iv'e looked at How can I query Android contact based on a phone number? - but not quite sure howto go about it.
Asked
Active
Viewed 2,201 times
1
2 Answers
1
Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(origin));
Cursor phonesCursor = context.getContentResolver().query(phoneUri, new String[] {PhoneLookup.DISPLAY_NAME}, null, null, null);
if(phonesCursor != null && phonesCursor.moveToFirst()) {
displayName = phonesCursor.getString(0); // this is the contact name
}//end if
Go this eventually.

Bo Persson
- 90,663
- 31
- 146
- 203

GrumP
- 1,183
- 6
- 19
- 43
-1
That question had the answer and posted the code.
Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(mNumber));
Cursor phonesCursor = managedQuery(phoneUri, new String[] {PhoneLookup.DISPLAY_NAME}, null, null, null);
if(phonesCursor != null && phonesCursor.moveToFirst()) {
String displayName = phonesCursor.getString(0); // this is the contact name

Robby Pond
- 73,164
- 16
- 126
- 119
-
Gives an error with the managedQuery line. Not really sure what to do with it. – GrumP Feb 17 '11 at 14:43
-
@GrumP I had an extra 'null' in the function call. Try it again. – Robby Pond Feb 17 '11 at 14:52
-
@GrumP managedQuery() is a member of the Activity class. That code assumes you are calling it from your Activity. – Robby Pond Feb 17 '11 at 18:38
-
The class I am working with extends Broadcast receiver, how should I go about getting the code you posted to work? – GrumP Feb 17 '11 at 18:39
-
Tried this is a class extending activity, got no compiler errors, but app crashes at run time. Very frustrating. Have spent a long time at it now. – GrumP Feb 23 '11 at 22:26