问题描述
- 如何调用android通讯录?
- 我正在写一个android应用程序,需要调用手机的通讯录。我需要调用通讯录的功能,选择一个联系人,然后将联系人的名字返回到我的应用程序中。下边是我在网上找的代码,但是不起作用。
import android.app.ListActivity;import android.content.Intent;import android.database.Cursor;import android.os.Bundle;import android.provider.Contacts.People;import android.view.View;import android.widget.ListAdapter;import android.widget.ListView;import android.widget.SimpleCursorAdapter;import android.widget.TextView;public class Contacts extends ListActivity { private ListAdapter mAdapter; public TextView pbContact; public static String PBCONTACT; public static final int ACTIVITY_EDIT=1; private static final int ACTIVITY_CREATE=0; // 当activity第一次创建完成后调用 @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); Cursor C = getContentResolver().query(People.CONTENT_URI null null null null); startManagingCursor(C); String[] columns = new String[] {People.NAME}; int[] names = new int[] {R.id.row_entry}; mAdapter = new SimpleCursorAdapter(this R.layout.mycontacts C columns names); setListAdapter(mAdapter); } @Override protected void onListItemClick(ListView l View v int position long id) { super.onListItemClick(l v position id); Cursor C = (Cursor) mAdapter.getItem(position); PBCONTACT = C.getString(C.getColumnIndex(People.NAME)); Intent i = new Intent(this NoteEdit.class); startActivityForResult(i ACTIVITY_CREATE); }}
解决方案
我不能100%的确定你的示例代码应该是做什么的,但是下边的摘要应该能够帮助你调用通讯录的功能,选择一个联系人,然后将你联系人的名字返回到你的app中。
这个过程有三步:
1)
许可
向你的应用程序的manifest中添加读取通讯录数据的许可
<uses-permission android:name=""android.permission.READ_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; }}
解决方案二:
对于android 2.2 Froyo版本我是这样做的:基本上是用eclipse来创建一个类,像:public class SomePickContactName 继承Activity
然后插入这个代码。记得加private类变量和在我的版本代码中被引用的常量
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intentContact = new Intent(Intent.ACTION_PICK ContactsContract.Contacts.CONTENT_URI); startActivityForResult(intentContact PICK_CONTACT);}public void onActivityResult(int requestCode int resultCode Intent intent) { if (requestCode == PICK_CONTACT) { getContactInfo(intent); }}protected void getContactInfo(Intent intent){ Cursor cursor = managedQuery(intent.getData() null null null null); while (cursor.moveToNext()) { String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); if ( hasPhone.equalsIgnoreCase(""1"")) hasPhone = ""true""; else hasPhone = ""false"" ; if (Boolean.parseBoolean(hasPhone)) { Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI nullContactsContract.CommonDataKinds.Phone.CONTACT_ID +"" = ""+ contactIdnull null); while (phones.moveToNext()) { phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); } phones.close(); } Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URInullContactsContract.CommonDataKinds.Email.CONTACT_ID + "" = "" + contactIdnull null); while (emails.moveToNext()) { emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); } emails.close(); Cursor address = getContentResolver().query( ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI null ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + "" = "" + contactId null null); while (address.moveToNext()) { poBox = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX)); street = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET)); city = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY)); state = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION)); postalCode = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE)); country = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY)); type = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE)); } } cursor.close();}
时间: 2025-01-02 06:59:06