querying contacts - SOMETIMES returns empty cursor - android

I'm trying to query contact's display name:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQ_CODE_PICK_CONTACT:
if (resultCode == Activity.RESULT_OK) {
Uri contactUri = data.getData();
ContentResolver cr = getActivity().getContentResolver();
Cursor c = cr.query(contactUri, null, null, null, null);
if (c != null && c.moveToFirst()) {
//get the contact name
}
}
break;
}
}
Now here is the problem:
For some contacts the cursor returns empty, and I don't figure out why.
I checked the value of contactUri, it looks like: content://com.android.contacts/data/3032
The Uri looks the same for all types of contacts I tried - facebook, google, phone etc.
For some contacts the cursor returns with a result, which is good and I can extract the name. But for others it's somehow empty even though the ContentUri is exactly the same, It was originated from Intent.getData().
Here are some facts that may have something to do with this weird problem:
All the contacts that has empty cursor are facebook contacts. the Uri looks like above.
Not all the facebook contacts cause this: I have HTC One X, which on the phonebook I can "Link" between contacts if the OS finds a relation between them (say, if it detects the similar phone number for a gmail account and facebook acount, it suggests me to "link" between them). Only the facebook contacts who were NOT "Linked" returns empty.
Right now im out of ideas. Did anyone encountered this before?
Thanks in advance.

I had the same problem on an HTC Incredible S, which makes me think that it might be an HTC phones issue. Anyway, the workaround I ended up using is by retrieving the phone number from the bundle you get back with the data intent.
final String phoneNumber = data.getStringExtra("android.intent.extra.PHONE_NUMBER");
at that point you'll need to do some "reverse logic" to fetch the other data of the contact using PhoneLookup.

Related

How can show device primary contact viewer to show ONE contact with phone number

I'm writing an app and I need to show a contact to user. I have the phone number and I can make a query to get the contact detail. Like this:
ContentResolver cr = context.getContentResolver();
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 null;
}
String contactName = phoneNumber;
if (cursor.moveToFirst()) {
contactName = cursor.getString(cursor
.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
}
BUT I need to show the DEVICE primary contact viewer and I do't know How?
Like this:
This is all documented in official Android docs, Common Intents, chapter "Contacts/People App".
EDIT
But unfortunately I can not access to your link , nor google documents also in my area !!
Here're quotes from linked docs:
View a contact
To display the details for a known contact, use the ACTION_VIEW action and specify the contact with a content: URI as the intent data.
There are primarily two ways to initially retrieve the contact's URI:
Use the contact URI returned by the ACTION_PICK, shown in the previous section (this approach does not require any app permissions).
Access the list of all contacts directly, as described in Retrieving a List of Contacts (this approach requires the READ_CONTACTS permission).
Example:
public void viewContact(Uri contactUri) {
Intent intent = new Intent(Intent.ACTION_VIEW, contactUri);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
Edit an existing contact
To edit a known contact, use the ACTION_EDIT action, specify the contact with a content: URI as the intent data, and include any known contact information in extras specified by constants in ContactsContract.Intents.Insert.
There are primarily two ways to initially retrieve the contact URI:
Use the contact URI returned by the ACTION_PICK, shown in the previous section (this approach does not require any app permissions).
Access the list of all contacts directly, as described in Retrieving a List of Contacts (this approach requires the READ_CONTACTS permission).
Note: Extras - One or more of the extras defined in ContactsContract.Intents.Insert so you can populate fields of the contact details.
public void editContact(Uri contactUri, String email) {
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setData(contactUri);
intent.putExtra(Intents.Insert.EMAIL, email);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
For more information about how to edit a contact, read Modifying Contacts Using Intents.

Getting details of edited contact back using ACTION_EDIT and startActivityForResult()

I am maintaining a list of contacts for my application. First I pick contact using ACTION_PICK with an intent. I am saving these contacts in my local database along with CONTACT_ID. Now user may choose to edit any contact to add some extra information that my app needs. I fire that edit intent with ACTION_EDIT. I have two questions here
I am saving CONTACT_ID to my database in order to use in edit intent. First question is does this id changes after edition ? because It so happens that I am unable to open contact editor for same contact again.
Second question is how can I read back all the details of this edited contact ? I am getting this back in my onActivityResult(). I have tried to use same code which i am using in case of picking but data structure returned in case of edit is different it seems.
you can get any data from contacts using a cursor like
String[] projection = new String[] {
ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup._ID};
// query time
Cursor cursor = _context.getContentResolver()
.query("ContactsContract.Contacts.CONTENT_URI",
projection, null, null, null);
if(cursor != null) {
while ( cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex(
ContactsContract.PhoneLookup.DISPLAY_NAME));
// do other stuff
}
}
cursor.close();
take a look at https://developer.android.com/training/contacts-provider/retrieve-details.html

Is there an accepted/received way of checking to see if a person (phone number) is in the Contact List?

Is there an accepted/received way of checking to see if a person (phone number) is in the Contact List?
I'm hoping there's something I can call like this:
bool bInContactList = InContactList("1415922353");
It is not as simple as you want but you can query a Contacts content provider for the contact associated with a phone number:
Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor c = getContentResolver().query(lookupUri, new String[] {PhoneLookup._ID}, null, null, null);
if (c.getCount() > 0) {
// there is some contact
} else {
// there is no contacts with phoneNumber
}
The application needs android.permission.READ_CONTACTS permission to access contacts data.
You can check Android Developer site for further references about content providers and android.providers package documentation for the list of available standard providers in Android.
I am afraid you did not provide enough information about the language you are talking about. If you use Java -for example- you have a contains method (common to all collections) so, if you want to know if a certain String is contained in a collection you could do it by invoking this method:
boolean found = someCollection.conmtains("1415922353");

For Android, is there a way to determine how many text messages have been sent to a contact?

I'm reading contact data from ContractContacts, and can lookup TIMES_CONTACTED (which is useful to me), but this field only applies to calls to that contact. I'm also interested in the number of times a contact has been contacted via SMS or email.
Does anyone know if this information is available? I've been searching but haven't come across anything.
For SMS, you'll want to access the inbox located at content://sms/inbox directly and perform a database query to count the number of rows corresponding to the matching contact.
Something like:
String personAddress = addressFromContact();
Uri smsUri = new Uri("content://sms");
if (smsUri != null) {
Cursor smsCursor = getContentResolver().query(smsUri, null, "address=?", new String[] {personAddress}, null);
int smsCount = smsCursor.getCount();
}

Android : Check phone number present in Contact List ? (Phone number retrieve from phone call)

I make a BroadcastReceiver to receive Phone number of the person who call me
<intent-filter>
<action
android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
How to check if the phone number receive is on my contact list ?
Do you have a tip to know if this phone number exist on contact list with out loading contact list ?
I don't want more information, just if this phone number exist.
If it's not possible, and I must load contact list, how to do it on BroadcastReceiver ?
When I try to do getContentResolver, it's not working because I'm on BroadcastReceiver and not inside Activity...
Thanks for your help
public boolean contactExists(Context context, String number) {
// number is the phone number
Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };
Cursor cur = context.getContentResolver().query(lookupUri, mPhoneNumberProjection, null, null, null);
try {
if (cur.moveToFirst()) {
cur.close();
return true;
}
} finally {
if (cur != null)
cur.close();
}
}
return false;
}
I think it's important to say that you need to add the following in your manifest file.
<uses-permission android:name="android.permission.READ_CONTACTS" />
for 1 you should have a look at the recommended ContactsContract.PhoneLookup provider
A table that represents the result of looking up a phone number, for example for caller ID. To perform a lookup you must append the number you want to find to CONTENT_FILTER_URI. This query is highly optimized.
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor mycursor=resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME,...
if (mycursor!=null && mycursor.moveToFirst()) {
// record exists
}
for 2 you can use the context from the onReceive method to call methods that belong to Context
ContentResolver cr=context.getContentResolver();
I suggest you to use Phone.CONTENT_FILTER_URI instead of PhoneLookup.CONTENT_FILTER_URI
because PhoneLookup can be empty and you will get no result from time to time (tested on LG-P500, froyo)
The problem on my device happens for example when:
switch to airplane mode
use the default message application to send a sms (will be queued).
use PhoneLookup.CONTENT_FILTER_URI to query for a contact
Not all devices seems to be affected
Using PhoneLookup.CONTENT_FILTER_URI the returned cursor is always empty.
Using Phone.CONTENT_FILTER_URI everything is ok (you find the contact if any).
Therefore I suggest you to always use Phone.* Uris except when you really need to use PhoneLookup.*... Which usually is just address book synchronization related stuff (and most of the times is not what you are interested in).

Categories

Resources