我在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
}
所以,我必须得到联系人姓名和联系电话号码!
答案 0 :(得分:0)
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");
}