标签:
这个过程有三步:
1)许可向你的应用程序的manifest中添加读取通讯录数据的许可
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
2)调用Contact Picker
在你的activity中,创建一个Intent,用来请求系统查找一个能够执行一个从Contacts URI的项目中发出的PICK动作的activity,
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
调用startActivityForResult在Intent中传递(和一个整数的请求代码,在这个例子中是PICK CONTACT )。这将导致android启动一个已经被等级的activity,用来支持在People.CONTENT URI上的ACTION_PICK ,然后当按下选择(或者取消)的时候返回到这个activity。startActivityForResult(intent, PICK_CONTACT);
3)仍然在你的activity中,重写onActivityResul方法来监听在你的第二个步骤中启动的activi‘select a contact‘ activity的回调。你应该检查反馈的请求代码是否匹配你的期望值,结果代码是否是 RESULT_OK。
通过在数据Intent参数中调用getData()你可以获得选择的联系人的URI。为了获得选择的联系人的名字你需要使用URI从返回的游标中创建一个新的查询和提取的参数。
@Overridepublic void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));}}break;}}
然后是例子奉上:
import android.app.Activity; import android.content.ContentResolver; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.view.View; import android.widget.EditText; public class MainActivity extends Activity { EditText text; String username,usernumber; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text = (EditText)findViewById(R.id.number); } public void click(View v){ startActivityForResult(new Intent( Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI), 0); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == Activity.RESULT_OK) { ContentResolver reContentResolverol = getContentResolver(); Uri contactData = data.getData(); @SuppressWarnings("deprecation") Cursor cursor = managedQuery(contactData, null, null, null, null); cursor.moveToFirst(); username = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); Cursor phone = reContentResolverol.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null); while (phone.moveToNext()) { usernumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); text.setText(usernumber+" ("+username+")"); } } } }
简洁的?
private static final int REQUEST_CONTACT = 1; private void startContact() { Intent intent = new Intent(); intent.setAction(Intent.ACTION_PICK); intent.setData(ContactsContract.Contacts.CONTENT_URI); startActivityForResult(intent, REQUEST_CONTACT); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CONTACT) { if (resultCode == RESULT_OK) { if (data == null) { return; } Uri result = data.getData(); contactId = result.getLastPathSegment(); contactName = getPhoneContacts(contactId); } }
标签:
原文地址:http://www.cnblogs.com/stareblankly/p/5009710.html