M Permission Basic | Ask for READ_CONTACTS permission Android M


As we know that Android M is introduced a new feature for end user to grant permission to access their valuable file contents. Being a developer we should also aware that how can we request permission from user to access required contents i.e contact numbers, Camera files, Gps location etc.

We are going to fetch contact number from contact directory for that we need to grant for
READ_CONTACTS permission.

public class LoginActivity extends AppCompatActivity{  
 /** * Id to identity READ_CONTACTS permission request. */  
 private static final int REQUEST_READ_CONTACTS = 0;  
 @Overrideprotected void onCreate(Bundle savedInstanceState) {  
   super.onCreate(savedInstanceState);  
   setContentView(R.layout.activity_login);  
   populateAutoComplete();  
 }  
 private void populateAutoComplete() {  
   if (!mayRequestContacts()) {  
     return;  
   }  
   //To Do tasks  
 }  
 private boolean mayRequestContacts() {  
   if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {  
     return true;  
   }  
   if (checkSelfPermission(READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {  
     return true;  
   }  
   if (shouldShowRequestPermissionRationale(READ_CONTACTS)) {  
     Snackbar.make(mEmailView, R.string.permission_rationale, Snackbar.LENGTH_INDEFINITE)  
         .setAction(android.R.string.ok, new View.OnClickListener() {  
           @Override          @TargetApi(Build.VERSION_CODES.M)  
           public void onClick(View v) {  
             requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);  
           }  
         });  
   } else {  
     requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);  
   }  
   return false;  
 }  
 /** * Callback received when a permissions request has been completed. */@Overridepublic void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,  
                     @NonNull int[] grantResults) {  
   if (requestCode == REQUEST_READ_CONTACTS) {  
     if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {  
       populateAutoComplete();  
     }  
   }  
 }  
 }  

Here "checkSelfPermission()" method check that given permission is granted by user or not if not then request for the prmission by using "requestPermissions()" which takes permissions array and request code just like startActivityForResult(). So simple we will get return callback at "onRequestPermissionsResult()" which passes you request code and permission array. If any permission need explanation we check this by "shouldShowRequestPermissionRationale()".

Also You need to know about permission groups as well like if any permission has been granted by user from permission group then you no need to request for another permission from that same group. 
Please visit here for more info.


For Pre Marshmallow we need to request methods from AppCompat support library i.e.

private boolean mayRequestContacts() {  
   if (ContextCompat.checkSelfPermission(READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {  
     return true;  
   }  
   if (ActivityCompat.shouldShowRequestPermissionRationale(READ_CONTACTS)) {  
     Snackbar.make(mEmailView, R.string.permission_rationale, Snackbar.LENGTH_INDEFINITE)  
         .setAction(android.R.string.ok, new View.OnClickListener() {  
           @Override          @TargetApi(Build.VERSION_CODES.M)  
           public void onClick(View v) {  
             ActivityCompat.requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);  
           }  
         });  
   } else {  
     ActivityCompat.requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);  
   }  
   return false;  
 }  


Please feel free to comment and write me your queries directly to me I will try to revert you. 


Previous
Next Post »