歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android選擇聯系人

Android選擇聯系人

日期:2017/3/1 9:43:08   编辑:Linux編程

其實Android裡沒有難的技術,看它的文檔就可了。下面算是直接從它的文檔裡抄的例子,稍微加點東西。

Android實現批量添加聯系人到通訊錄 http://www.linuxidc.com/Linux/2014-05/101853.htm

Android手機聯系人數據庫分析 http://www.linuxidc.com/Linux/2013-12/94346.htm

Android調用系統聯系人返回聯系人結果 http://www.linuxidc.com/Linux/2013-05/84294.htm

Android 由data 獲取聯系人信息 http://www.linuxidc.com/Linux/2013-04/82696.htm

Android使用Contact數據模型來批量插入聯系人 http://www.linuxidc.com/Linux/2011-12/48574.htm

發出action請求

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);
}
}
}

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved