【发布时间】:2016-08-28 18:03:46
【问题描述】:
在我的 android 应用程序中,我有一个人员列表(姓名和电话号码),我让用户通过从他们的联系人列表中导入一个人来将一个人添加到应用程序的数据库中。一切正常,但用户可以单击按钮导入一个新的人,然后返回,因此联系人意图将在没有实际选择联系人的情况下关闭。
所以我需要知道在我的代码中放置 if 或其他内容的位置,以便查看用户是否选择了联系人。
private void pickContact() {
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request it is that we're responding to
if (requestCode == PICK_CONTACT_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// Get the URI that points to the selected contact
Uri contactUri = data.getData();
String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor cursor = getContentResolver().query(contactUri, null, null, null, null);
cursor.moveToFirst();
// Retrieve the name from the DISPLAY_NAME column
String name = cursor.getString(cursor.getColumnIndex(ontactsContract.Contacts.DISPLAY_NAME));
// Retrieve the phone number from the NUMBER column
int column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String number = cursor.getString(column);
// update database
dbManager.addPerson(name, number, true);
}
}
}
那么我如何检查用户是否真的选择了联系人或返回?
【问题讨论】:
标签: android android-intent android-contacts back-button