android developer tiny share-20160818
来源:互联网 发布:sql server2008安装包 编辑:程序博客网 时间:2024/05/16 08:40
今天分享如何通过intent访问通讯录,以及如何访问通讯录的某部分数据,如只访问电话号码、只访问邮箱、只访问邮政编码。
Contacts/People App
Select a contact
To have the user select a contact and provide your app access to all the contact information, use the ACTION_PICK action and specify the MIME type to Contacts.CONTENT_TYPE.
The result Intent delivered to your onActivityResult() callback contains the content: URI pointing to the selected contact. The response grants your app temporary permissions to read that contact using the Contacts Provider API even if your app does not include the READ_CONTACTS permission.
Tip: If you need access to only a specific piece of contact information, such as a phone number or email address, instead see the next section about how to select specific contact data.
Action
ACTION_PICK
Data URI Scheme
None
MIME Type
Contacts.CONTENT_TYPE
Example intent:
static final int REQUEST_SELECT_CONTACT = 1;public void selectContact() { Intent intent = new Intent(Intent.ACTION_PICK); intent.setType(ContactsContract.Contacts.CONTENT_TYPE); if (intent.resolveActivity(getPackageManager()) != null) { startActivityForResult(intent, REQUEST_SELECT_CONTACT); }}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_SELECT_CONTACT && resultCode == RESULT_OK) { Uri contactUri = data.getData(); // Do something with the selected contact at contactUri ... }}
For information about how to retrieve contact details once you have the contact URI, read Retrieving Details for a Contact. Remember, when you retrieve the contact URI with the above intent, you do not need the READ_CONTACTS permission to read details for that contact.
Select specific contact data
To have the user select a specific piece of information from a contact, such as a phone number, email address, or other data type, use the ACTION_PICK action and specify the MIME type to one of the content types listed below, such as CommonDataKinds.Phone.CONTENT_TYPE to get the contact's phone number.
If you need to retrieve only one type of data from a contact, this technique with a CONTENT_TYPE from the ContactsContract.CommonDataKinds classes is more efficient than using the Contacts.CONTENT_TYPE (as shown in the previous section) because the result provides you direct access to the desired data without requiring you to perform a more complex query to Contacts Provider.
The result Intent delivered to your onActivityResult() callback contains the content: URI pointing to the selected contact data. The response grants your app temporary permissions to read that contact data even if your app does not include the READ_CONTACTS permission.
Action
ACTION_PICK
Data URI Scheme
None
MIME Type
CommonDataKinds.Phone.CONTENT_TYPE
Pick from contacts with a phone number.
CommonDataKinds.Email.CONTENT_TYPE
Pick from contacts with an email address.
CommonDataKinds.StructuredPostal.CONTENT_TYPE
Pick from contacts with a postal address.
Or one of many other CONTENT_TYPE values under ContactsContract.
Example intent:
static final int REQUEST_SELECT_PHONE_NUMBER = 1;public void selectContact() { // Start an activity for the user to pick a phone number from contacts Intent intent = new Intent(Intent.ACTION_PICK); intent.setType(CommonDataKinds.Phone.CONTENT_TYPE); if (intent.resolveActivity(getPackageManager()) != null) { startActivityForResult(intent, REQUEST_SELECT_PHONE_NUMBER); }}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_SELECT_PHONE_NUMBER && resultCode == RESULT_OK) { // Get the URI and query the content provider for the phone number Uri contactUri = data.getData(); String[] projection = new String[]{CommonDataKinds.Phone.NUMBER}; Cursor cursor = getContentResolver().query(contactUri, projection, null, null, null); // If the cursor returned is valid, get the phone number if (cursor != null && cursor.moveToFirst()) { int numberIndex = cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER); String number = cursor.getString(numberIndex); // Do something with the phone number ... } }}
- android developer tiny share-20160818
- android developer tiny share-20160627
- android developer tiny share-20160628
- android developer tiny share-20160629
- android developer tiny share-20160630
- android developer tiny share-20160701
- android developer tiny share-20160704
- android developer tiny share-20160705
- android developer tiny share-20160706
- android developer tiny share-20160707
- android developer tiny share-20160708
- android developer tiny share-20160711
- android developer tiny share-20160712
- android developer tiny share-20160713
- android developer tiny share-20160714
- android developer tiny share-20160715
- android developer tiny share-20160718
- android developer tiny share-20160719
- 苹果iPhone 7价格差很多,谨慎购买!
- 判断user 是否开启 delve 功能
- 程序打包
- Oracle数据库远程导入、导出
- session性能问题
- android developer tiny share-20160818
- C++泛型编程
- BabeLua在VS2012中点击lua按钮不显示Setting
- iOS ANCS学习
- 钱咖是真的吗?钱咖怎么提现?钱咖怎么收徒弟快?钱咖赚钱容易吗?
- 如何判断H5移动端是IOS还是安卓
- 用virtualenv建立多个Python独立开发环境
- Android各版本变化
- 为什么阿里做不好社交,腾讯做不好电商?