RequestPermissions not showing a dialog box - android

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.

Related

PHONE_CALL permission not working in Dexter

I have implemented Dexter in my app. It is work fine for CAMERA,EXTERNAL STORAGE and INTERNAL STORAGE permission. I want to call with PHONE_CALL permission with Dexter. When i call intent for phone call like this:
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + strNum));
startActivity(callIntent);
then startActivity shows warning Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with checkPermission) or explicitly handle a potential SecurityException less... (Ctrl+F1)
I don't understand that i have implement Dexter then why startActivity want self permission?
For API 23+ you should check for permission as:
if (mContext.checkSelfPermission(Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + strNum));
startActivity(callIntent):
}
Intent.ACTION_CALL intent, which requires a permission, namely the android.permission.CALL_PHONE one. But for sdk>=23 you need to check in runtime with Manifest.permission.CALL_PHONE. It is for targetedsdkversion 23 and above.
If you lower your targetedsdkversion below 23 then you don't need this persmission and Intent.ACTION_CALL will work fine.
I had a similar issue. For me, my justification was showing when I tried to make a call. I added the manifest tag:
<uses-permission android:name="android.permission.CALL_PHONE" />
Not sure why I had to do this in addition to using Dexter runtime checking, but it solved my issue.

Provide custom text for Android M permission dialog

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

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.

Android M permission dialog not showing

I'm currently trying to adapt my application to the new permissions model of Android M.
I'm collecting all the permissions I require, then run
Log.i("Permissions", "Requesting permissions: " + permissions);
requestPermissions(requiredPermissions.toArray(new String[requiredPermissions.size()]), requestCodeForPermissions);
requiredPermissions holds the permissions I need like android.permission.WRITE_EXTERNAL_STORAGE.
That routine is definitely executed as I have the Log line in the logcat:
08-07 12:52:46.469: I/Permissions(1674): Requesting permissions: android.permission.RECEIVE_BOOT_COMPLETED; android.permission.WRITE_EXTERNAL_STORAGE
But the permissions dialog never shows, let alone is onRequestPermissionsResult() called.
What am I doing wrong? Based on some tutorials I found I'm not missing anything.
I only have the emulator for testing, no physical device. This is the about screen from settings:
Image
It might be worth mentioning something else: If I try to open the overview of installed apps from the home screen I only get launcher3 has exited. I'm not sure if that might be related.
Does anybody have an idea why it's not showing?
I experienced the same issue but later I realized I forgot to add the permission to the manifest file. After adding the uses-permission tag, the system showed the dialog. Maybe helps someone.
The original answer helped me.
I fixed by adding tools:remove="android:maxSdkVersion" like this:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:remove="android:maxSdkVersion"/>
I experienced the same issue because I was using the negative value as a REQUEST_CODE.
requestPermissions(new String[]{android.Manifest.permission.CAMERA}, -1)
After using positive value, the system showed the dialog.
Hope it helps someone.
Based on the comment from Hilal (thanks a lot!):
In my case my app is indeed using tabhost and the permissions were requested from an Activity inside the tabhost. After starting a separate activity that requests the permissions it is working.
I just had the same problem.
My issue was that I wrote the permission at the wrong place in the manifest.
Make sure the uses permission is outside of application:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.android.gms.samples.vision.face.photo"
android:installLocation="auto"
android:versionCode="1"
android:versionName="1" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:hardwareAccelerated="true"
android:label="FacePhotoDemo"
android:allowBackup="true"
android:icon="#drawable/icon">
add
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
in AndroidManifest.xml
Note : The Permission which you want to get. Eg: android.permission.ACCESS_FINE_LOCATION etc.
I have also come across a situation where the permission dialog doesn't appear or the application crashes when using the <uses-permission-sdk23> element, however the cause appears to be a system bug on current 6.0 devices:
https://code.google.com/p/android/issues/detail?id=189841
Crash exception:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.packageinstaller/com.android.packageinstaller.permission.ui.GrantPermissionsActivity}: java.lang.NullPointerException: Attempt to get length of null array
I have the same issue and the problem is solved after adding the shouldShowRequestPermissionRationale like this:
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
} else {
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
}
To add to #shanraisshan's answer, the REQUEST_CODE actually has to be greater than 0, not just non-negative.
In our code, it was a simple spelling mistake.
We had:
<uses-permission android:name="android.permission.ACCESS_COURSE_LOCATION" />
It should be:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
I was doing two calls to requestPermissions(), one right after the other. This error message appeared in Logcat (note the misspelling on "request"):
Can reqeust only one set of permissions at a time
requestPermissions() is actually designed to take multiple requests at once; that's what the String array is for.
In my case, the permission I was requesting (WRITE_SETTINGS) was more special and required Settings Activity to launch. So dialog was not showing up.
I had to get its permission using the following code:
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + context.getPackageName()));
startActivityForResult(intent, CODE_WRITE_SETTINGS_PERMISSION);
Yet another cause for not getting the permission dialog to show when requesting a dangerous permission...
I had to do Build -> Clean Project and then Build -> Rebuild Project. I guess Android Studio didn't pick up on the changes I made in the manifest.
I had a similar issue caused by the wrong case of the permission constant in the manifest, I was using read_contacts in lower case:
<uses-permission android:name="android.permission.read_contacts" />
After changing read_contacts to uppercase it started working as expected
<uses-permission android:name="android.permission.READ_CONTACTS" />
Permissions are organised into categories so non-critical ones are granted without the dialog being shown.
I found this the hard way with internet permission, if you're having this issue then changing to a critical permission such as read_contacts will allow you to test your flow and reveal whether the issue is the permission being non-critical or something else.
Normal protection permissions are listed here
In the manifest, I changed
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="22" />
to
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
/>
Maybe that helps.
Add permission in the manifest file as well.
If anyone of you guys has an application that changes/modifies WRITE_SETTINGS and are facing this issue. Check out CommonsWare's Answer to WRITE_SETTINGS Ambiguity
I was facing this issue for 2 weeks and later realised that this issue was due to the fact that requestPermissions doesn't work for requesting WRITE_SETTINGS permission.
Hope this helps :D
In my case I had requested "ACCESS_COARSE_LOCATION" in my manifest file and then request for "ACCESS_FINE_LOCATION" permission in code that's why the Permission Dialog was not opening.
After searching a while it appears it is required to set the value compileSdkVersion to "android-MNC" or as of today to 23. That requires the Gradle build system which then seems to require Android Studio.
At least I couldn't find a single manual about how to set it outside the gradle files.
I did all the things said in above answers but still dialog was not showing and then I changed targetSdkVersion to 23 in gradle and it appeared . Hope this helps someone
#Override
public final void validateRequestPermissionsRequestCode(int requestCode) {
// We use 16 bits of the request code to encode the fragment id when
// requesting permissions from a fragment. Hence, requestPermissions()
// should validate the code against that but we cannot override it as we
// can not then call super and also the ActivityCompat would call back to
// this override. To handle this we use dependency inversion where we are
// the validator of request codes when requesting permissions in
// ActivityCompat.
if (!mRequestedPermissionsFromFragment
&& requestCode != -1 && (requestCode & 0xffff0000) != 0) {
throw new IllegalArgumentException("Can only use lower 16 bits for requestCode");
}
}
Had the same issue. Later I realized that we have to declare each and every permission in manifest (even if one is a subclass of another).
In my case I declared
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
in my manifest and was trying to access user's coarse location.
ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
Fixed the problem by adding coarse permission as well in manifest.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
Set your targetSdkVersion to 22.
had the same (i guess) problem and the solution was removing
import <app package>.Manifest;
autoimported by Android Studio at the top of the file and substitute it with
import android.Manifest;
and started working
In my case the solution is the string itself
android.permission.READ_CONTACTS
I did Manifest.permission.READ_CONTACTS which caused silence error (Noting show on the screen).
Make sure that this is correct
My Android targetSDK version is 28.
I don't see any pop ups shown to user requesting for permission which are listed in android Manifest.xml as .
During both installation, installing .apk using USB and installing app from google play store.
So I added below code in my activity it will ask user permission during runtime
ActivityCompat.requestPermissions(MyActivity.this,
new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
I was requesting permission through code, but had missed adding the <uses-permission ..> tag in manifest !
Change the final constant value to 1.
private static final int REQUEST_PERMISSION_WRITE = 1;
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_PERMISSION_WRITE);

java.lang.SecurityException trying to read from Android Contacts URI

I am trying to read Contact names, phone #'s, and emails from the ContactsContract URI, and I am getting a SecurityException when I try to run the program. I have set the permission in the AndroidManifest.xml file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.smumn.cs394"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
**<uses-permission android:name="android.pemission.READ_CONTACTS"/>**
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".ReadPhoneNumbers"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>`
The following is the application code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_list);
ContentResolver resolver = getContentResolver();
Cursor c = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
//[...] Work through data here`
I get a security exception on the last line (resolver.query()):
`03-08 07:41:40.812: ERROR/AndroidRuntime(416): FATAL EXCEPTION: main
03-08 07:41:40.812: ERROR/AndroidRuntime(416): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.smumn.cs394/edu.smumn.cs394.ReadPhoneNumbers}: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/contacts from pid=416, uid=10037 requires android.permission.READ_CONTACTS
[...]
03-08 07:41:40.812: ERROR/AndroidRuntime(416): Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/contacts from pid=416, uid=10037 requires android.permission.READ_CONTACTS
[...]
03-08 07:41:40.812: ERROR/AndroidRuntime(416): at edu.smumn.cs394.ReadPhoneNumbers.onCreate(ReadPhoneNumbers.java:30)
[...]`
I must be missing something, but I can't figure out what.
Requesting Permissions at Run Time
Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app.
If the permission you need to add isn't listed under the normal permissions, you'll need to deal with "Runtime Permissions". Runtime permissions are permissions that are requested as they are needed while the app is running. These permissions will show a dialog to the user, similar to the following one:
The first step when adding a "Runtime Permission" is to add it to the AndroidManifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.codepath.androidpermissionsdemo" >
<uses-permission android:name="android.permission.READ_CONTACTS" />
...
</manifest>
Next, you'll need to initiate the permission request and handle the result. The following code shows how to do this in the context of an Activity, but this is also possible from within a 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's 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's 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);
}
}
}
Make sure you add it outside of the application tag. While developing for a target platform of 2.3.3 using Eclipse on Ubuntu, I had permission failures in the log file that indicated I needed this exact line while working on something similar. It wasn't until I moved the *uses-permission...READ_CONTACTS* line to outside the application tag that things worked.
Hello Steven the debug log trace tells you that you need
... requires android.permission.READ_CONTACTS
so just try something by editing the Manifest.xml like adding another permission, let see if its not correctly readed.
and check this line without **
<uses-permission android:name="android.permission.READ_CONTACTS" />
dan
with the api 23, permission <uses-permission android:name="android.pemission.READ_CONTACTS"/> dont work, change the api level in the emulator for api 22(lollipop) or lower
If the device is running Android 6.0 or higher, and your app's target SDK is 23 or higher: The app has to list the permissions in the manifest, and it must request each dangerous permission it needs while the app is running. The user can grant or deny each permission, and the app can continue to run with limited capabilities even if the user denies a permission request.
EXAMPLE:
//thisActivity is the running activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an expanation 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, we can 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.
}
}
http://developer.android.com/training/permissions/requesting.html

Categories

Resources