checkSelfPermission method is not working in targetSdkVersion 22
The method Context#checkSelfPermission(String)
was added to the API 23. Also below API 23 it's pointless as the permission is always granted. Make a check for the API version before handling the permissions
private void insertDummyContactWrapper() {
// the only way we insert the dummy contact if if we are below M.
// Else we continue on and prompt the user for permissions
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
insertDummyContact();
return;
}
List<String> permissionsNeeded = new ArrayList<String>();
final List<String> permissionsList = new ArrayList<String>();
if (!addPermission(permissionsList, Manifest.permission.ACCESS_FINE_LOCATION))
permissionsNeeded.add("GPS");
if (!addPermission(permissionsList, Manifest.permission.READ_CONTACTS))
permissionsNeeded.add("Read Contacts");
if (!addPermission(permissionsList, Manifest.permission.WRITE_CONTACTS))
permissionsNeeded.add("Write Contacts");
if (permissionsList.size() > 0) {
if (permissionsNeeded.size() > 0) {
// Need Rationale
String message = "You need to grant access to " + permissionsNeeded.get(0);
for (int i = 1; i < permissionsNeeded.size(); i++)
message = message + ", " + permissionsNeeded.get(i);
showMessageOKCancel(message,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
}
});
return;
}
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
}
}
Reference: http://developer.android.com/reference/android/content/Context.html#checkSelfPermission(java.lang.String)
Android targetSdkVersion 23 checkSelfPermission method
If you target SDK 23 (Android 6), all of the permissions (in your manifest) are disabled by default, whereas if your targetSDK is 22 (Android 5.1) and your app is running on Android 6, all of the permissions are enabled by default when the user installs the app, and even if the user revokes the permissions later on, checkSelfPermission
returns incorrect value of PERMISSION_GRANTED
It is also available in the documentation of PermissionChecker
In the new permission model permissions with protection level dangerous are runtime permissions. For apps targeting M and above the user may not grant such permissions or revoke them at any time. For apps targeting API lower than M these permissions are always granted as such apps do not expect permission revocations and would crash. Therefore, when the user disables a permission for a legacy app in the UI the platform disables the APIs guarded by this permission making them a no-op which is doing nothing or returning an empty result or default error.
Android permissions checkSelfPermission doesn't work
Wow. All that time the problem was with AndroidManifest.xml. I specified minSdkVersion in the wrong place. I should've done that in uses-sdk xml tag, not in manifest tag, just like on the Android Developer website is written: https://developer.android.com/guide/topics/manifest/uses-sdk-element
I don't know where I picked up an idea that these attributes should be written in manifest tag. Perhaps this is what gradle is doing - it transfers the attributes when compiling the apk..
Edit: In addition, one have to restart the app after permission has been granted. Otherwise, checkSelfPermission will inform that acccess has been granted, but the app will not have any access to the files: Writing external storage doesn't work until I restart the application on Android M
checkSelfPermission returning PERMISSION_GRANTED for revoked permission with targetSdkVersion = 22
Use the android.support.v4.content.PermissionChecker
https://developer.android.com/reference/android/support/v4/content/PermissionChecker.html
ActivityCompat.requestPermissions for targetSdkVersion 30 is not working
On android 11 requests for background location permission will be implicitly denied if user haven't granted foreground location permission first.
Requests containing both foreground and background location permission at the same time are deemed invalid:
If your app targets Android 11 (API level 30) or higher, the system
enforces this best practice. If you request a foreground location
permission and the background location permission at the same time,
the system ignores the request and doesn't grant your app either
permission.
See Request location access at runtime
checkSelfPermission: How to do it prior to API 22
You should check permissions only for API >= 23.For API < 21 will use install-time requests
Android Studio isn't recognizing the checkSelfPermission
So far so good, running it on less than M OS will get:
INSTALL_FAILED_OLDER_SDK
That is because setting compileSdkVersion
to android-MNC
forces the minSdkVersion
to MNC
by default. There are recipes for changing that behavior.
But when I changed it to... The Android Studio isn't recognizing the checkSelfPermission (...) method
checkSelfPermission()
was introduced in the M Developer Preview and does not exist on older versions of Android.
android ContextCompat.checkSelfPermission() not found
If you have migrated from eclipse adding the below line wont work
compile 'com.android.support:appcompat-v7:23.0.1'
While migrating, it adds appcompatv4 as an external dependent library.
Make sure you have deleted the appcompatv4 library from the libs folder and then it should start working
Related Topics
How to Limit the Height of Spinner Drop Down View in Android
Android Runonuithread Explanation
Login Failed Invalid Key Error with Facebook Sdk
How to Parse Same Name Tag in Android Xml Dom Parsing
How to Animate View.Setvisibility(Gone)
Customize Android Intent.Action_Send
Android Character by Character Display Text Animation
How to Perform an SQLite Query Within an Android Application
Programmatically Set Android Phone's Background
How to Access Fragment's Child Views Inside Fragment's Parent Activity
Intent to Launch the Clock Application on Android
My Current Location Always Returns Null. How to Fix This
Actionbaractivity Cannot Resolve a Symbol
Which Architecture Patterns Are Used on Android
Android: When Should I Use a Handler() and When Should I Use a Thread