Contract Content Provider 通讯📞内容提供器Demo
可以直接简单Demo下载 方便下一步的研究和学习;
编码中遇到的问题:
- permisson权限问题:
java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{8dc4b6b 28533:com.example.contractcontentprovidertest/u0a392} (pid=28533, uid=10392) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
解决方法,大致的思路就是通过询问user给不给权限。权限问题在android6后是特别重视的!这里也可以直接代码动态申请权限:
代码如下;
private static String[] PERMISSIONS_STORAGE = {//动态申请权限列表
// "android.permission.WRITE_CONTACTS",
"android.permission.READ_CONTACTS"};//读通讯录权限的权限名
private PermissionListener mListener;
private void getReadPhone() {
if (Build.VERSION.SDK_INT >= 23) {//判断当前系统是不是Android6.0
requestRuntimePermissions(PERMISSIONS_STORAGE, new PermissionListener() {
@Override
public void granted() {
Log.e("读通讯录权限申请", "已通过");
// GetNumber.getNumber(SplashActivity.this);
}
@Override
public void denied(List<String> deniedList) {
//权限申请未通过
Log.e("读通讯录权限申请", "已拒绝");
}
});
}
}
/**
* 申请权限
*/
public void requestRuntimePermissions(
String[] permissions, PermissionListener listener) {
mListener = listener;
List<String> permissionList = new ArrayList<>();
// 遍历每一个申请的权限,把没有通过的权限放在集合中
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(MainActivity.this, permission) !=
PackageManager.PERMISSION_GRANTED) {
permissionList.add(permission);
} else {
mListener.granted();
}
}
// 申请权限
if (!permissionList.isEmpty()) {
ActivityCompat.requestPermissions(MainActivity.this,
permissionList.toArray(new String[permissionList.size()]), 1);
}
}
也可以直接手动给权限(我就是直接在真机中给相应的权限进行操作的)重要的是读取
- 读取该用户的所有电话号码
最开始的代码:
//遍历-获取每一个通讯者的相关信息
while(cursor.moveToNext()){
Map man = new HashMap();
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
man.put("name",name);
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor cursor_phone = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, ContactsContract.CommonDataKinds.Phone._ID +"=?" ,new String[]{id},null);
ArrayList<String> phones = new ArrayList<>();
while(cursor_phone.moveToNext()){
String phone = cursor_phone.getString(cursor_phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER));
if(phone != null){
phones.add(phone);
}
}
man.put("phones",phones);
contracts.add(man);
}
这个我是按照网上的教程一步步走下去的,但是并不理想!在debug的时候发现phones就是空的!
初步认定是在内容提供器中没有读取到我想要的数据;
故尝试参考资料1-用list存放该用户下的多个电话号码 中,改写其中的查询代码。修改完后的代码:的确是这个query的问题。已解决!
//遍历-获取每一个通讯者的相关信息
while(cursor.moveToNext()){
Map man = new HashMap();
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
man.put("name",name);
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.NAME_RAW_CONTACT_ID));
Cursor cursor_phone = resolver.query(ContactsContract.Data.CONTENT_URI,null, ContactsContract.Contacts.Entity.RAW_CONTACT_ID +"=?" ,new String[]{id},null);
ArrayList<String> phones = new ArrayList<>();
while(cursor_phone.moveToNext()){
String phone = cursor_phone.getString(cursor_phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER));
if(phone != null){
phones.add(phone);
}
}
man.put("phones",phones);
contracts.add(man);
}
- 出现其他的权限错误
java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/contacts from pid=483, uid=10290 requires android.permission.READ_CONTACTS, or grantUriPermission()
原来是我的Permission 写错了!还是要仔细——我少写了Read的权限!正确如下:
//这个最好动态申请权限;这样的用户交互才会有一定的效果!
private static String[] PERMISSIONS = {
"android.permission.READ_CONTACTS",
"android.permission.WRITE_CONTACTS"
};