Provide custom text for Android M permission dialog - android

Is it possible to provide a custom text for the system dialog which is displayed when the user is asked to grant permission?

No, you can't customize the text of the dialog, but you can provide an explanation before request the permission. Quoting from developer.android.com:
Request Permissions
If your app needs a dangerous permission that was listed in the app
manifest, it must ask the user to grant the permission. Android
provides several methods you can use to request a permission. Calling
these methods brings up a standard Android dialog, which you cannot
customize.
Explain why the app needs permissions
In some circumstances, you might want to help the user understand why
your app needs a permission. For example, if a user launches a
photography app, the user probably won't be surprised that the app
asks for permission to use the camera, but the user might not
understand why the app wants access to the user's location or
contacts. Before you request a permission, you should consider
providing an explanation to the user. Keep in mind that you don't want
to overwhelm the user with explanations; if you provide too many
explanations, the user might find the app frustrating and remove it.
One approach you might use is to provide an explanation only if the
user has already turned down that permission request. If a user keeps
trying to use functionality that requires a permission, but keeps
turning down the permission request, that probably shows that the user
doesn't understand why the app needs the permission to provide that
functionality. In a situation like that, it's probably a good idea to
show an explanation.
To help find situations where the user might need an explanation,
Android provides a utiltity method,
shouldShowRequestPermissionRationale(). This method returns true if
the app has requested this permission previously and the user denied
the request.

We cannot customize request permission dialog but we can provide user a custom explanation that why we are requesting below is the method with custom explanation
private void checkForCameraPermission() {
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
alertBuilder.setCancelable(true);
alertBuilder.setTitle("Camera permission necessary");
alertBuilder.setMessage("FITsociety need camera permission to read barcode.");
alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(BarCodeScannerActivity.this,
new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
}
});
AlertDialog alert = alertBuilder.create();
alert.show();
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
// MY_PERMISSIONS_REQUEST_CAMERA is an
// app-defined int constant. The callback method gets the
// result of the request.
}
} else {
setBarCodeScannerView();
}
}
the above method check whether permission is already granted if not then it check if custom explanation is required with this method
ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)
the documentation for this method is here shouldShowRequestPermissionRationale() this method return true only if user deny to permission dialog or user close the permission from the setting of the application if user did so then show alert dialog with custom explanation and proceed further hope it works

Related

Checking permission

My app uses Internet access to display ads so I have the Internet permission declared in Manifest.
With latest Android releases, the user can disable this permission.
My problem is that if the user cancels this permission, the app is not able to load ads.
I know I can check if there is any current connection, in that case I will not try to load the app. But I want to know if the user has denied that permission in Android settings.
The idea is to prompt the user to enable it or the app will not run.
I add a screenshot of the Huawei Android Pie where the user can modify these settings
You just need to check first that whether the user enable (Allow the permission) or not. If not then redirect the user to the phone setting so that the user allow that permission otherwise just finish the app.
if (ContextCompat.checkSelfPermission(this,Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED )
{
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
// if user blocked permission
android.app.AlertDialog.Builder dlgAlert = new android.app.AlertDialog.Builder(this);
dlgAlert.setPositiveButton("Take me to app settings",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent);
}
});
dlgAlert.setNegativeButton("Cancel",null);
dlgAlert.setCancelable(true);
dlgAlert.setIcon(R.drawable.appicon);
dlgAlert.setMessage("The app needs the permission please allow it other wise you cannot proceed.");
dlgAlert.setTitle("Oops!");
dlgAlert.create().show();
}
}
else
{
finish();
}
If internet connection is all you want to check, you dont have to check permissions for this because Internet is not a critical permission. What you can simply do is use ConnectivityManager class to check if the device has active internet connection or not and then write your logic accordingly.
Refer: This on how you can check for internet connectivity.

How to implement shouldShowRequestPermissionRationale?

How do I implement the shouldShowRequestPermissionRationale()?
So far I am leaving it blank in my code and it works fine, but how can I actually use this method to show my rationale? I understood it should somehow shoe a pop-up dialog with something I write. Or, I can actually do what ever I want there instead of a pop-up dialog? for example show a TextView instead?
This is my code:
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
} else {
// Permission has already been granted
}
How do I make it show my custom rationale in case the user previously clicked Deny? How do I show my own dialog the second time?
shouldShowRequestPermissionRationale() is used as a hint that the user may need more info about why you are asking for a particular permission. Typically this method will return true if the user has denied the permission at least once AND they didn't click don't ask again. You can do whatever you want in your UI, most display a popup, but it's up to you. shouldShowRequestPermissionRationale() is not a replacement for requestPermissions(), you'll still need to request permissions after you show your rationale and the user reads it.

RequestPermissions not showing a dialog box

I found a lot of similar topics with the same threat but I still can't find a solution for my problem. I wrote this code to grant the writing permission to the app but there is no dialog box showing. I get in the monitor the No writing permission messages.
if(ContextCompat.checkSelfPermission(getContext(),Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) {
Log.i("permissions", "No writing permission");
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 225);
I added the permission in the AndroidManifest fils
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Changed the target sdk targetSdkVersion 23, and I am using android 6.0.1.
Edit:
I also tied this code but it still not working
requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 225);
I had a similiar case. Do not try to call it with ActivityCompat from a Fragment. Instead use the given requestPermissions method from the Fragment e.g.
requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 225);
RequestPermissions Dialog will not be shown only in below 2 cases on device >= 6.0:
1) Either you have already given permission to the any of the Dangerous Permissions within the Category you are asking for.
2) You had clicked Never Ask Again checkbox when the dialog had shown previously.

Android on demand permission request

How to handle don't ask me again in android on demand permission request. Is their any best practice if the user chooses don't ask me again and later if he want to get access any best practice suggestion? Thanks.
shouldShowRequestPermissionRationale will help you with this.
shouldShowRequestPermissionRationale() will be used to check whether user have selected Never ask again. It will return value as True or False, If user have selected Never ask before it will return false, else will return true.
boolean shouldShow = shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE);
if (!shouldShow) {
// show some dialog which will give information about required permission, so that user can understand what is need of that permission.
}
on button click of dialog, you can redirect user to setting screen of app.
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,Uri.fromParts("package", getPackageName(), null));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
If you are working for pre-marshmallow, you need to use ActivityCompat.shouldShowRequestPermissionRationale()

Android M, not ask every start for storage permission

My app needs to access the user's storage in near to every activity. How can I access the storage without making the app ask at every app startup for the permission to access it with the new Android M permission model? (Read and write)
Thanks!
If you do not want to prompt the user "on every app startup to give memory access to be able to do anything", then don't do that. Only call requestPermissions() if checkSelfPermission() returns PERMISSION_DENIED.
#Override
public void onCreate(Bundle icicle) {
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)==
PackageManager.PERMISSION_GRANTED) {
init();
}
else {
requestPermissions(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },
REQUEST_STORAGE);
}
}
(native API Level 23 methods shown; you may want to use ContextCompat andActivityCompat` for backwards compatibility)
Or, only call requestPermissions() if the user does something positive in the UI that needs WRITE_EXTERNAL_STORAGE (e.g., clicks an action bar item) and you do not already have the permission.

Categories

Resources