手机联系人权限 - 获取手机联系人

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ZWName/article/details/56677664
    /*
     * 检查联系人权限
     */
    private void getPhone() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            //如果没有授权,则请求授权
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, MY_PERMISSIONS_REQUEST_CALL_CAMERA);
        } else {
            //有授权
            getPerson(ImportKinsfolkActivity.this);
        }
    }
    /**
     * 遍历手机联系人
     *
     * @param context
     * @return
     */
    public String getPerson(Context context) {
        String ConstrantsJson = "";
        // JSONObject jsonObject = new JSONObject();
        JSONArray array = new JSONArray();
        Cursor cursor = context.getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
                null, null);
        jobjList = new ArrayList<>();
        while (cursor.moveToNext()) {
            int indexPeopleName = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            int indexPhoneNum = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            String strPeopleName = cursor.getString(indexPeopleName).replace(" ", "");
            String strPhoneNum = cursor.getString(indexPhoneNum).replace(" ", "");
            if (Tools.isMobileNO(strPhoneNum)) {
                ImportKinsfolkBean bean = new ImportKinsfolkBean();
                bean.name = strPeopleName;
                bean.phone = strPhoneNum;

                mList.add(bean);
//                JSONObject jsonObjectItem = new JSONObject();
//                try {
//                    jsonObjectItem.put("Name", strPeopleName);
//                    jsonObjectItem.put("Phone", strPhoneNum);
//                    array.put(jsonObjectItem);
//                    jobjList.add(jsonObjectItem);
//                } catch (Exception e) {
//                }
            }
        }
        if (!cursor.isClosed()) {
            cursor.close();
            cursor = null;
        }
        ConstrantsJson = array.toString();
//        invite_array = array;
//        if(array.length() > 500) {
//            invite_num = Math.ceil(array.length() / 500.0);
//        }
        tv_num.setText("发现"+mNum+"条疑似亲人记录");
        return ConstrantsJson;
    }

猜你喜欢

转载自blog.csdn.net/ZWName/article/details/56677664