【问题标题】:How can i get the contact phone number by LookUpKey?如何通过 LookUpKey 获取联系电话号码?
【发布时间】:2015-06-19 21:10:41
【问题描述】:

我已经在 AndroidManifest 文件中注册了一个意图,以使我的应用在用户在联系人应用中选择联系人时弹出,并且我成功检索了查找键,但我找不到获取联系选择的详细信息,我已经使用光标查看了其他一些信息,但是当我执行查询功能时,我的应用程序崩溃了,这是我检索查找键的代码

    Intent intent = getIntent();
    String action = intent.getAction();
    Uri contactUri = intent.getData();
    if (Intent.ACTION_VIEW.equals(action)) {
        // here i don't know what to use to search for the contact by the contactUri
    }

所以,我必须得到联系人姓名和联系电话!

【问题讨论】:

标签: android android-intent android-contacts


【解决方案1】:
Intent intent = getIntent();
String action = intent.getAction();
Uri contactUri = intent.getData();
if (Intent.ACTION_VIEW.equals(action)) {

       Log.d("START","Getting all Contacts");
       ArrayList<PhoneContactInfo> arrContacts = new ArrayList<PhoneContactInfo>();
       PhoneContactInfo phoneContactInfo=null;    
       Cursor cursor = context.getContentResolver().query(contactUri , new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone._ID}, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
       cursor.moveToFirst();
       while (cursor.isAfterLast() == false)
         {
           String contactNumber= cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));  
           String contactName =  cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
          int phoneContactID = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));


          phoneContactInfo = new PhoneContactInfo();
          phoneContactInfo.setPhoneContactID(phoneContactID);             
          phoneContactInfo.setContactName(contactName);                   
        phoneContactInfo.setContactNumber(contactNumber); 
        if (phoneContactInfo != null)
        {
             arrContacts.add(phoneContactInfo);
         }
       phoneContactInfo = null; 
       cursor.moveToNext();
}       
cursor.close();
cursor = null;
Log.d("END","Got all Contacts");

}

【讨论】:

    猜你喜欢
    • 2016-12-26
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-11
    • 2011-07-30
    • 2015-11-10
    相关资源
    最近更新 更多