联系人列表在Android M中没有响应

时间:2016-06-15 07:06:09

标签: java android android-permissions

我正在使用RecyclerView显示来自移动联系人的联系人,  这里的问题是“当我尝试将我的应用程序在Android M上运行时它没有响应它会抛出错误

Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{3b9a84a 17730:com.strobilanthes.contactdetials/u0a224} (pid=17730, uid=10224) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS"

我的代码如下:

 public class MainAct extends AppCompatActivity implements AdapterView.OnItemClickListener {
    private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 100;
    List<String> phno1 = new ArrayList<String>();
    Button select;
    private EditText edtContactFilter;
    private RecyclerView recyclerView;
    private ICEAdapter iceAdapter;
    ArrayList<ContactList> contactLists;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_display);

        recyclerView = (RecyclerView) findViewById(R.id.recycle_view);
        getAllContacts(this.getContentResolver());
        updateCabLsit(contactLists);

        select = (Button) findViewById(R.id.button1);
        select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuilder checkedcontacts = new StringBuilder();

                Toast.makeText(MainAct.this, checkedcontacts, Toast.LENGTH_SHORT).show();
            }
        });

        edtContactFilter = (EditText) findViewById(R.id.edt_contact_filter);

    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
    }

    public void getAllContacts(ContentResolver cr) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, PERMISSIONS_REQUEST_READ_CONTACTS);

        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.SORT_KEY_PRIMARY);
            contactLists = new ArrayList<ContactList>();
            while (phones.moveToNext()) {
                String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                System.out.println(".................." + phoneNumber + " >> " + name);

                if (!phno1.contains(phoneNumber)) {
                    //name1.add(name);
                    phno1.add(phoneNumber);

                    ContactList c = new ContactList();
                    c.setIceName(name);
                    c.setIceNumber(phoneNumber);
                    contactLists.add(c);

                }
            }
            phones.close();
        } else {
            Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.SORT_KEY_PRIMARY);
            contactLists = new ArrayList<ContactList>();
            while (phones.moveToNext()) {
                String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                System.out.println(".................." + phoneNumber + " >> " + name);

                if (!phno1.contains(phoneNumber)) {
                    //name1.add(name);
                    phno1.add(phoneNumber);

                    ContactList c = new ContactList();
                    c.setIceName(name);
                    c.setIceNumber(phoneNumber);
                    contactLists.add(c);

                }
            }
            phones.close();
        }
    }

    public class ICEAdapter extends RecyclerView.Adapter<ICEAdapter.ViewHolder> {

        private ArrayList<ContactList> contactLists;
        private Context mContext;

        public ICEAdapter(Context context, ArrayList<ContactList> contactList) {
            this.contactLists = contactList;
            this.mContext = context;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.ice_items, viewGroup, false);
            return new ViewHolder(v);
        }


        @Override
        public void onBindViewHolder(ViewHolder holder, final int position) {
            holder.txtName.setText(contactLists.get(position).getIceName());
            holder.txtNumber.setText(contactLists.get(position).getIceNumber());
        }

        @Override
        public int getItemCount() {
            return contactLists == null ? 0 : contactLists.size();
        }

        public ContactList getItem(int position) {
            return contactLists.get(position);
        }

        public class ViewHolder extends RecyclerView.ViewHolder {

            TextView txtName;
            TextView txtNumber;
            CheckBox chkSelect;

            public ViewHolder(View itemView) {
                super(itemView);

                txtName = (TextView) itemView.findViewById(R.id.txt_name);
                txtNumber = (TextView) itemView.findViewById(R.id.txt_number);
                chkSelect = (CheckBox) itemView.findViewById(R.id.chk_select);
            }

        }
    }

    private void updateCabLsit(ArrayList<ContactList> contactLists) {
        recyclerView.setHasFixedSize(true);
        LinearLayoutManager linearLayout = new LinearLayoutManager(this);
        linearLayout.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayout);

        iceAdapter = new ICEAdapter(this, contactLists);
        recyclerView.setAdapter(iceAdapter);
        iceAdapter.notifyDataSetChanged();
    }}

0 个答案:

没有答案