Tôi cố gắng để chọn địa chỉ liên lạc với số điện thoại only.And tôi sau this codeAndroid: Liên hệ Picker Intent | không thể nhanh chóng loại Uri
static final int PICK_CONTACT_REQUEST = 1; // The request code
...
private void pickContact() {
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, new Uri("content://contacts"));
pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}
Nhưng thật không may, cho thấy nó có lỗi: Mã Cannot instantiate the type Uri
Thật sự tôi đã làm việc khác mà đang hoạt động hoàn hảo nhưng gặp sự cố khi chọn Danh bạ email. Tôi chỉ cần số điện thoại.
Intent intentContact = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
intentContact.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(intentContact, PICK_CONTACT);
và tại onReceive()
, phương pháp này được gọi là
public void getContactInfo(Intent intent) {
ContentResolver cr = getContentResolver();
cursor = cr.query(intent.getData(), null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
if (Integer
.parseInt(cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId, null, null);
while (phones.moveToNext()) {
phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
} else {
snipp.showAlertDialog(getApplicationContext(), "No Number",
"Cannot read number", false);
}
}
cursor.close();
}
gì dòng mã là gây ra ngoại lệ này? bạn có thể dán toàn bộ ngăn xếp trong câu hỏi không? bạn có định nghĩa onActivityResult() của mình không? –
Đã sửa lỗi. :) Đó là một sửa chữa đơn giản .. thực sự sai lầm của tôi .. cảm ơn thời gian quý báu của bạn :) –