这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录 前一修订版 后一修订版 | 前一修订版 | ||
wiki:book:notes:android:第一行代码-android-第2版-郭霖:第7章 [2022/04/07 11:13] 博丽幻月 [delete()] |
wiki:book:notes:android:第一行代码-android-第2版-郭霖:第7章 [2022/04/07 14:39] (当前版本) 博丽幻月 [创建自己的内容提供器] |
||
---|---|---|---|
行 82: | 行 82: | ||
</ | </ | ||
==== 读取系统联系人 ==== | ==== 读取系统联系人 ==== | ||
+ | 读取联系人方法:\\ | ||
+ | <code java> | ||
+ | private void readContacts() { | ||
+ | Cursor cursor = null; | ||
+ | try { | ||
+ | // 查询联系人数据 | ||
+ | cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, | ||
+ | if(cursor != null) { | ||
+ | while (cursor.moveToNext()) { | ||
+ | // | ||
+ | String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); | ||
+ | // | ||
+ | String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); | ||
+ | } | ||
+ | } | ||
+ | } catch (Exception e) { | ||
+ | e.printStackTrace(); | ||
+ | } finally { | ||
+ | if (cursor != null) { | ||
+ | cursor.close(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | 判断是否有权限和读取:\\ | ||
+ | <code java> | ||
+ | if(ContextCompat.checkSelfPermission(this, | ||
+ | ActivityCompat.requestPermissions(this, | ||
+ | } else { | ||
+ | readContacts(); | ||
+ | } | ||
+ | </ | ||
+ | ===== 创建自己的内容提供器 ===== | ||
+ | ==== 创建内容提供器步骤 ==== | ||
+ | **TBD**\\ |