设为首页 加入收藏

TOP

Android选择联系人
2014-11-23 22:55:08 来源: 作者: 【 】 浏览:9
Tags:Android 选择 联系人

Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
startActivityForResult(pickContactIntent, 11);


接收回复


protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == 11 && resultCode == RESULT_OK)
{
Uri contactUri = data.getData();
// We only need the NUMBER column, because there will be only one row in the result

String[] projection = {
ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
};
//String[] projection = {Phone.NUMBER};


// Perform the query on the contact to get the NUMBER column
// We don't need a selection or sort order (there's only one result for the given URI)
// CAUTION: The query() method should be called from a separate thread to avoid blocking
// your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
// Consider using CursorLoader to perform the query.
Cursor cursor = getContentResolver()
.query(contactUri, projection, null, null, null);
cursor.moveToFirst();


// Retrieve the phone number from the NUMBER column
int indexPhoneNumber = cursor.getColumnIndex(Phone.NUMBER);
int indexDisplayName = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
String phoneNumber = cursor.getString(indexPhoneNumber);
String displayName = cursor.getString(indexDisplayName);


if(phoneNumber != null && displayName != null)
{
if(phoneNumber.startsWith("+86"))
phoneNumber = phoneNumber.substring(3);

EditText textNumber = (EditText)findViewById(R.id.text_number);
textNumber.setText("\"" + displayName + "\"" + phoneNumber);
}
}
}


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇如何在Java中执行Hive命令或HiveQL 下一篇数组的指针特性

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: