Sunday, July 12, 2020

How to request permissions in Android API 23 and above.

From Android API 23, we need to request for permission in Activity class and user has "Allow" for permission in order to use the permission.

You need to declare the permission in manifest file and need to request in Activity as well.

In this blog we will request for 2 permissions   "READ_PHONE_STATE" and "ACCESS_FINE_LOCATION"

Declare the permissions in manifest inside application TAG
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>


String permissionsRequired[];

@Overrideprotected void onCreate(Bundle savedInstanceState)
{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);

        permissionsRequired = new String[]{ Manifest.permission.ACCESS_FINE_LOCATION, 
        Manifest.permission.READ_PHONE_STATE};

     if (android.os.Build.VERSION.SDK_INT >= 23) {
     if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != 
        PackageManager.PERMISSION_GRANTED &&
            ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != 
          PackageManager.PERMISSION_GRANTED) {
        showPermissionDialog();
    }

}
void showPermissionDialog()
{
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Need Permission");
    builder.setMessage("This app needs following permission.\n" +
            "1: Location Permission \n" +
            "2: Phone Permission\n\n"+
            "Please click on allow to give permissions.");
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
            requestForPermissions(permissionsRequired);


        }
    });

    builder.show();
}

// request for permissions
void requestForPermissions(String permissions[])
{
    ActivityCompat.requestPermissions(GPSCoordinatesMainActivity.this, permissions, 1);
}

// handle whether user allowed and denied the permission
@Overridepublic void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
{
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    permissionDeniedCount++;
   
    if (requestCode == 1) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED &&
                grantResults[1] == PackageManager.PERMISSION_GRANTED) {
            //The External Storage Write Permission is granted to you... Continue your left job...            getGPSLocation();
        }  else {
            //Show Information about why you need the permission            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("You denied the required Permission");
            builder.setMessage(""Please click on allow to give permissions.");
            builder.setPositiveButton("Give Permissions", new DialogInterface.OnClickListener() {
                @Override                public void onClick(DialogInterface dialog, int which) {
                    requestForPermissions(permissionsRequired);
                    dialog.cancel();

                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                    finish();
                }
            });
            builder.show();

        }
    }

}




No comments:

Post a Comment