2011-09-06 57 views
0

在我的应用程序中,用户可以点击他手机中的联系人,然后查看他的联系表。我在HTC上测试没有问题。然后我对中兴通讯进行了测试,出现了一件奇怪的事情:如果有些人点击他们查看他们的联系表单,另一个联系表单会出现另一个人。还有一个功能可以将这些人称为我没有遇到任何问题的人。所以例如我点击蒂姆伯顿查看他的联系表,我得到蒂姆伯顿的联系表。然后我点击布鲁斯威利斯,我得到马克斯佩恩的联系表。Android联系表问题

什么重要的也许是,在中兴的情况下,有接触从旧手机进口(塞班),也许这会导致某种问题。所以布鲁斯威利斯可能是从该手机进口的(手机的拥有者并不知道)。 我在没有导入联系人的情况下在另一个中兴通讯上测试了该应用。联系表没有问题。所以无论我的代码需要改进还是这是一个错误。

这是我的代码跳到一个选定的触点的接触片: (所述NAME1变量具有所选择的触点,例如布鲁斯W的名称)

infobtn.setOnClickListener(new View.OnClickListener() 
     { 
      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); 
       } 

      } 
     }); 

由于提前,

erdomester

回答

0

我找到了方法。原因是我使用了不推荐的意图。这是正确的代码:

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); 
        }