Created
September 17, 2016 08:34
-
-
Save shubham08gupta/f7f1ad0f86e1f20709604f6a7a586c5f to your computer and use it in GitHub Desktop.
A simple class to manage android permissions in Marshmallow version (Android 6.0) or above.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.logintutorial; | |
import android.Manifest; | |
import android.annotation.TargetApi; | |
import android.content.pm.PackageManager; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.v4.app.ActivityCompat; | |
import android.support.v4.content.ContextCompat; | |
import android.support.v7.app.AppCompatActivity; | |
public class AskPermission extends AppCompatActivity { | |
private static final int READ_CONTACTS_REQUEST_CODE = 1; | |
@TargetApi(Build.VERSION_CODES.M) | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_ask_permission); | |
/*check if we have permission or not | |
returns Permission_GRANTED if we have, otherwise Permission_DENIED | |
*/ | |
// no need to check android version as these classes are of support library which already checks android version | |
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS); | |
if (permissionCheck == PackageManager.PERMISSION_GRANTED) { | |
// perform your work | |
} else { | |
// request for permission | |
boolean isDeniedPreviously = ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CONTACTS); | |
if (isDeniedPreviously) { | |
// show an explanation as to why you need this permission and again request for permission | |
// if don't ask again box is checked, and we have again asked for permission it will directly call | |
// onRequestPermissionResult with Permission_DENIED result | |
} else { | |
// request for permission | |
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, READ_CONTACTS_REQUEST_CODE); // shows the dialog | |
} | |
} | |
} | |
@Override | |
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | |
super.onRequestPermissionsResult(requestCode, permissions, grantResults); | |
if (requestCode == READ_CONTACTS_REQUEST_CODE) { | |
// if request is cancelled grantResults array is empty | |
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | |
// perform your work as permission is granted | |
} else { | |
// permission is denied so disable any functionality the app will use because of this permission | |
//your app might show a dialog or snackbar explaining why it could not perform the user's requested action that needs that permission. | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment