Alex Tech Alex Tech - 6 months ago 27
Android Question

Android no permission to read from phonebook

I'm new on android. I want to make a function that check if I already have a phone number in my phonebook.
I added read contacts permission on my manifest:

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


I can't get the permission to read the contacts and my app crash every time.

Answer

Basically, you have to check if you have a permission or not before you doing something from the dangerous permissions list

int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
    Manifest.permission.WRITE_CALENDAR);

If it returns that your app is not granted then you need to ask for it in the runtime.

ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.READ_CONTACTS},
            MY_PERMISSIONS_REQUEST_READ_CONTACTS);

So, put the code above after your first "if" statement in "else" block.

Let me know if you have any more questions.