EDIT: SOLVED IT 🙁 AFTER SPENDING HOURS ON IT, IT WAS THE SIMPLEST OF SOLUTIONS. CORRECT CODE BELOW.
Ok. So I have the query working where I can look someone up by their phone number. What I want to do is if the phone number I look up doesn’t exist, it just returns the phone number. Here’s what I have so far:
Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor c = context.getContentResolver().query(lookupUri, new String[]{PhoneLookup.DISPLAY_NAME}, null, null, null);
while(c.moveToNext()) {
conname = c.getString(c.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
}
if (conname==null || conname.equals("") || conname.equals("null")) {
conname = phoneNumber;
}
if (messages.length > -1) {
smsToast = conname + ":\n'" + messages[0].getMessageBody() + "'";
Toast.makeText(context, smsToast, Toast.LENGTH_LONG).show();
Log.e("Text Thing", conname + " ugh");
Log.e("Text Thing", phoneNumber);
}
I tried everything to turn conname (the contacts name) back to the phoneNumber. It says conname = null if it doesn’t return anything on the toast. So what am I missing?
If
c.moveToNext()returnsfalsethe first time, then there’s no chance of setting a nullconnameback to the phone number. You can usec.moveToFirst()to test whether the cursor is empty and assign the phone number toconnamein that case.