When you try to read contacts in Android Marshmallow (Android M), the Android application crashes,

I have a target application for SDK version 23 (Android Marsmallow)

When I tried to read the contact from my application, I got this exception, although I gave READ_CONTACTS permission in the Android manifest file. I think a simple attempt to catch would not be the proper handling.

Exception Details:

Called: java.lang.SecurityException: permission denied: read com.android.providers.contacts.ContactsProvider2 uri content: //com.android.contacts/data from pid = 8373, uid = 10152 requires an android. READ_CONTACTS or grantUriPermission ()

My permissions for the manifest:

<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.READ_CONTACTS"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
+4
source share
2 answers

Runtime permissions

If the permission you need to add is not specified in the usual permissions, you need to deal with "Runtime Permissions". Run-time permissions are permissions that are requested as needed while the application is running. These permissions will show the user a dialog similar to the following:

enter image description here

The first step when adding "Runtime Permission" is to add it to AndroidManifest:

  <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.codepath.androidpermissionsdemo" > <uses-permission android:name="android.permission.READ_CONTACTS" /> ... </manifest> 

You will need to initiate a permission request and process the result. The following code shows how to do this in the context of an Activity, but it is also possible from within the fragment.

  // MainActivity.java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // In an actual app, you'd want to request a permission when the user performs an action // that requires that permission. getPermissionToReadUserContacts(); } // Identifier for the permission request private static final int READ_CONTACTS_PERMISSIONS_REQUEST = 1; // Called when the user is performing an action which requires the app to read the // user contacts public void getPermissionToReadUserContacts() { // 1) Use the support library version ContextCompat.checkSelfPermission(...) to avoid // checking the build version since Context.checkSelfPermission(...) is only available // in Marshmallow // 2) Always check for permission (even if permission has already been granted) // since the user can revoke permissions at any time through Settings if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { // The permission is NOT already granted. // Check if the user has been asked about this permission already and denied // it. If so, we want to give more explanation about why the permission is needed. if (shouldShowRequestPermissionRationale( Manifest.permission.READ_CONTACTS)) { // Show our own UI to explain to the user why we need to read the contacts // before actually requesting the permission and showing the default UI } // Fire off an async request to actually get the permission // This will show the standard permission request dialog UI requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, READ_CONTACTS_PERMISSIONS_REQUEST); } } // Callback with the request from calling requestPermissions(...) @Override public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) { // Make sure it our original READ_CONTACTS request if (requestCode == READ_CONTACTS_PERMISSIONS_REQUEST) { if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { Toast.makeText(this, "Read Contacts permission granted", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "Read Contacts permission denied", Toast.LENGTH_SHORT).show(); } } else { super.onRequestPermissionsResult(requestCode, permissions, grantResults); } } } 
+8
source

All Articles