如果我获得许可,我该如何联系联系人?

时间:2018-03-28 10:37:26

标签: android listview android-permissions

我正在为Google Play撰写应用程序。我必须得到用户的许可,因为我不会关闭我的应用程序。我的目的是第一次用户启动我的应用程序。我想获取他/她的联系人权限然后将她/他的联系人加载到自定义列表视图。但是当我需要用户的许可时,我的联系人没有加载到我的自定义Listview。我该如何修复它?

此处我的权限是在xml文件中

<uses-permission android:name="android.permission.READ_CONTACTS"
    />

    <uses-permission android:name="android.permission.VIBRATE"></uses-permission>
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

我的请求权限方法:

   if (ContextCompat
            .checkSelfPermission(MainActivity.this,
                    Manifest.permission.READ_CONTACTS) != (int) PackageManager.PERMISSION_GRANTED &&
            ContextCompat.checkSelfPermission(MainActivity.this,
                    Manifest.permission.CALL_PHONE)
                    != (int)PackageManager.PERMISSION_GRANTED) {

        // Check if user has opted "Never show again"
        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.READ_CONTACTS) ||
                ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.CALL_PHONE)) {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(new String[] {
                        Manifest.permission.READ_CONTACTS,
                        Manifest.permission.CALL_PHONE
                }, requestCode);
            }
        }
    } else {


        getNumber(this.getContentResolver());   // it is taking method of contacts


    }



        chosinglist = (ListView) findViewById(R.id.chosing);

 chosinglist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

GetNumber方法:

private void getNumber(ContentResolver contentResolver) {
        Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        if (cursor.moveToFirst()) {
            // ArrayList<String> alContacts = new ArrayList<String>();
            do {
                String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

                if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                    Cursor pCur = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
                    while (pCur.moveToNext()) {
                        String phoneNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                        String name = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

                        listte.add(name);
                        listtearama.add(phoneNumber);
                        //  alContacts.add(contactNumber);
                        break;
                    }
                    pCur.close();
                }

            } while (cursor.moveToNext());
        }

        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.checkrow,
                R.id.checkedTextView2, listte);

        kaydet.setEnabled(false);

        chosinglist.setAdapter(adapter); }

2 个答案:

答案 0 :(得分:1)

我认为您在获取联系人后正在初始化ListView

试试这个:

chosinglist = (ListView) findViewById(R.id.chosing);
chosinglist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

if (permissionNotGranted)
{

}
else
{
    getNumber(this.getContentResolver());   // it is taking method of contacts
}

答案 1 :(得分:0)

请试试这个:

if (ContextCompat
             .checkSelfPermission(MainActivity.this,
    Manifest.permission.READ_CONTACTS) != (int) PackageManager.PERMISSION_GRANTED &&
            ContextCompat.checkSelfPermission(MainActivity.this,
    Manifest.permission.CALL_PHONE)
            != (int)PackageManager.PERMISSION_GRANTED) {

        // Check if user has opted "Never show again"
        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.READ_CONTACTS) ||
                ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.CALL_PHONE)) {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(new String[] {
                        Manifest.permission.READ_CONTACTS,
                        Manifest.permission.CALL_PHONE
                }, requestCode);
            }else{
                getContactDetails();
            }

        }
    } else {
        getContactDetails();  // it is taking method of contacts
    }


    public void getContactDetails() {
            ContentResolver cr = getActivity().getContentResolver();
            String[] PROJECTION = new String[]{ContactsContract.RawContacts._ID,
                    ContactsContract.Contacts.DISPLAY_NAME,
                    ContactsContract.Contacts.PHOTO_ID,
                    ContactsContract.CommonDataKinds.Email.DATA,
                    ContactsContract.CommonDataKinds.Phone.NUMBER,
                    ContactsContract.RawContacts.VERSION};
            String order = "CASE WHEN "
                    + ContactsContract.Contacts.DISPLAY_NAME
                    + " NOT LIKE '%@%' THEN 1 ELSE 2 END, "
                    + ContactsContract.Contacts.DISPLAY_NAME
                    + ", "
                    + ContactsContract.CommonDataKinds.Phone.DATA
                    + " COLLATE NOCASE";
            String filter = ""+ ContactsContract.Contacts.HAS_PHONE_NUMBER + " > 0 and " + ContactsContract.CommonDataKinds.Phone.TYPE +"=" + ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE;
            Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, filter, null, order);

            for (int i = 0; i < cur.getColumnCount(); i++) {
                Timber.e("column " + i + "=" + cur.getColumnName(i));
            }

            if (cur.moveToFirst()) {
                do {
                    String name = cur.getString(1);
                    String number = cur.getString(4);

                    number = Utils.removeExtraCharFromString(number);

                    if (number.length() > 8) {
                        mContactNumbers = mContactNumbers + number + ",";
                        PhoneContacts phoneContacts = new PhoneContacts();
                        try {
                            phoneContacts.setName(URLEncoder.encode(name, "UTF-8"));
                            phoneContacts.setNumber(number);
                            phoneContacts.setNameInitial(String.valueOf(phoneContacts.getName().charAt(0)));

                            listOfContactsRaw.add(phoneContacts);
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }
                } while (cur.moveToNext());
            }
            cur.close();

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Set set = new TreeSet(new Comparator() {
                        @Override
                        public int compare(Object o1, Object o2) {

                            PhoneContacts phoneContacts1 = (PhoneContacts) o1;
                            PhoneContacts phoneContacts2 = (PhoneContacts) o2;
                            if (phoneContacts1.getNumber().equalsIgnoreCase(phoneContacts2.getNumber())) {
                                return 0;
                            }
                            return 1;
                        }
                    });
                    set.addAll(listOfContactsRaw);

                    System.out.println("\n***** After removing duplicates *******\n");

                    listOfContacts = new ArrayList(set);

                    initRecyclerView();
                }
            }, 200);
        }