Android contact sheet problem
in my app the user can click on a contact in his phone and then view his contact sheet. I tested in on my HTC without problem. Then I tested in on ZTE and a strange thing emerged: in case of some persons when I click on them to view their contact sheet, another contact sheet comes up of a different person. There is also a feature to call these people where I have not experienced any problems. So e.g. I click on Tim Burton to view his contact sheet and I get Tim Burton's contact sheet. Then I click on Bruce Willis and I get Max Payne's contact sheet.
What maybe essential is that in case of the ZTE there were contacts imported from an older phone (with Symbian) and maybe this causes some kind of problem. So Bruce Willis may have been imported from that phone (the owner of the phone does not know that). I tested the app on another ZTE without imported contacts. There was no problem with the contact sheets. So either my code needs improvement or this is a bug.
Here is my code to jump to the contact sheet of a selected contact: (the name1 variable has the name of the selected contact e.g. Bruce W)
infobtn.setOnClickListener(new View.OnClickListener()开发者_如何学Go
{
public void onClick(View v)
{
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0)
{
while (cur.moveToNext()) {
id_contact = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
name_contact = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (name_contact.equals(name1))
{
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id_contact}, null);
id_contact2 = id_contact;
while (pCur.moveToNext()){
}
pCur.close();
}
}
Intent intent_contacts = new Intent(Intent.ACTION_VIEW, Uri.parse("content://contacts/people/" + id_contact2));
startActivity(intent_contacts);
}
}
});
Thanks in advance,
erdomester
I found the way. The reason was that me using a deprecated intent. This is the correct code:
String contactid26 = null;
ContentResolver contentResolver = getContentResolver();
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phonenumintolist));
Cursor cursor =
contentResolver.query(
uri,
new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID},
null,
null,
null);
if(cursor!=null) {
while(cursor.moveToNext()){
String contactName = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
contactid26 = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup._ID));
}
cursor.close();
}
if (contactid26 == null)
{
Toast.makeText(DetailedCallHistory.this, "No contact found associated with this number", Toast.LENGTH_SHORT).show();
}
else
{
Intent intent_contacts = new Intent(Intent.ACTION_VIEW, Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactid26)));
//Intent intent_contacts = new Intent(Intent.ACTION_VIEW, Uri.parse("content://contacts/people/" + contactid26)); //deprecated!
startActivity(intent_contacts);
}
精彩评论