requestPermissions() method is showing error warning in Activity - android

I used requestPermissions() method in fragment and its working fine without showing any error or warning but when i use the same code in Activity for same purpose its working fine but showing an error warning. The following code i used in both fragment and Activity
requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE, CAMERA}, PERMISSION_REQUEST_CODE);
Its showing like this
Code is running correctly but its showing like this only in Activity not in fragment, Why and how can i fix this ?.

In your AndroidManifest.xml you have specified android:minSdkVersion = "21" but this API is only available on SDK versions 23 and up.
You have to check the android version first or your app will crash on android versions 5.0 (SDK 21) and 5.1 (SDK 22)
For cheking and supporting different android versions refer to https://developer.android.com/training/basics/supporting-devices/platforms

Your minimum api level version is 21 but requestPermission method works from version level 23.
So, to use requestPermission() method you must handle the version code-
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
requestPermission(............................)
}

Problem : Runtime Permission is part of MarshMallow & Above. Your current min API is 21(Lollipop) which does not have runtime permission.
Solution: Either you need to check whether current API is above Marshmallow or use ActivityCompat
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, PERMISSION_REQUEST_CODE);
}
Or
ActivityCompat.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, PERMISSION_REQUEST_CODE);

Related

Android- Check for install package permissions for API level below 26

So I want to check for android install permission of an app, weather granted or not, in Android API level below 26. This is my code it is working in Android API 26.
if(getPackageManager().canRequestPackageInstalls()){ //doing someting}
From this doc at https://developer.android.com/reference/android/content/pm/PackageManager.html#canRequestPackageInstalls()
I have found out that "canRequestPackageInstalls" has been added from API 26 is there an alternative for this for packages below API 26?
Below API 26, the permission is granted by default.
Try this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (!getPackageManager().canRequestPackageInstalls()) {
startActivityForResult(newIntent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES).setData(Uri.parse(String.format("package:%s", getPackageName()))), 1);
} else {
//do something
}
} else {
//do something
}

Permissions are granted by default

I'm not sure i fully understand this. So, for the <= 21 API version we can just use AndroidManifest.xml to request permissions, but Lollipop and higher APIs we have Requesting permission on runtime feature. So i'm using it with this simpe code:
if (Build.VERSION.SDK_INT >= 23) {
mPermissionsToBeAsked.clear();
for (String permission : AudioRecordingThread.PERMISSIONS_NEEDED) {
if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
mPermissionsToBeAsked.add(permission);
}
} ....
Then, if that list is not empty i'm requesting them :
if (mPermissionsToBeAsked.size() > 0) {
requestPermissions(mPermissionsToBeAsked.toArray(new String[0]), AUDIO_PERMISSIONS_REQUEST_CODE);
}
But, for some reason, on devices, for example, like Samsung Galaxy S7 with Android 6.0.1, all the permissions grandted by default when app is installed. So i want to know why, BUT, it's there is an even bigger concerne, when i go to my application in Application Manager and manually removing Microphone permision, in the app checkSelfPermission(permission) is still returning GRANTED. So the questions:
Why on devices with API level Lollipop and higher all permissions are still granted by default and above code won't add anything into mPersmissionToBeAsked?
Why if i manually removing permission with title MICROPHONE in Application manager checkSelfPermission(android.permission.RECORD_AUDIO) still returns GRANTED?
Just Cross verify in your app gradle file the targetsdk version is greater than 22.
defaultConfig {
// -----
targetSdkVersion 23
//----
}
If it is less than 23 than permission will automatically been granted to your app.
First of all it's Android M and above that handles permission granting. And that means you should have
targetSdkVersion 23
or above. Otherwise the system considers that the developper did not target this version, meaning that the developper does not check for permissions.

Use PopupWindow.setWindowLayoutType() on API < 23

It says method call requires API level 23. My code below,
But it is not possible because, following is from android official android.widget.Editor.java source file in API 19.
Also associated constants TYPE_APPLICATION_SUB_PANEL was added from api level 1. Also I used this successfully on Xamarin under api 14.
What is wrong here?
It simply show that setWindowLayoutType method does not available for API level 14.
you have to add API level condition.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
mContainer.setWindowLayoutType(WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL);
}
Did you install api 23 sdk? You must download from android sdk manager.

Android M Error on api 22, and requestPermissions() method not found

I'm working in API 22, but I want to compile my project in Android M 6.0, I have this code:
Declared at the top:
private static final String[] REQUIRED_PERMISSIONS = new String[]{"READ_EXTERNAL_STORAGE"};
private static final int REQUEST_PERMISSIONS = (Integer) null;
And on my onCreate():
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
LinkedList<String> missingPermissions = new LinkedList<>();
for(String p : REQUIRED_PERMISSIONS){
if(checkCallingOrSelfPermission(p) != PackageManager.PERMISSION_GRANTED){
missingPermissions.add(p);
}
}
if(!missingPermissions.isEmpty()){
String[] mpArray = new String[missingPermissions.size()];
missingPermissions.toArray(mpArray);
requestPermissions(mpArray, REQUEST_PERMISSIONS);
}
}
I was inspired here for checking my problem
and in Eclipse is giving me an error on Build.VERSION_CODES.M(M not found), and then, the callback method requestPermissions(mpArray, REQUEST_PERMISSIONS) isn't found too, any suggestion?
If I'm working on API 22, and I'm compiling with Android 6.0 M. How I can solve the issue for the dangerous permissions like READ_EXTERNAL_STORAGE correctly on API 22?
As per the documentation, Build.VERSION_CODES.M and requestPermissions() were added in API level 23.
Since you are compiling with API level 22, those simply do not exist.
To access APIs introduced in API level 23, you need to compile with API 23. You cannot access these APIs if you continue to compile with API 22.
Note that simply compiling with API 23 will not affect the way your application behaves on any devices, it simply opens up the newer APIs for your use on devices running at least API 23.
if I'm working on API 22, and I'm compiling with Android 6.0 M, how i can do for solve the issue for the dangerous permissions like READ_EXTERNAL_STORAGE correctly on API 22?
Devices running API 22 will continue to use the old install-time model for permissions. Nothing has changed for devices running API 22 and below. Only devices running API 23 use the new runtime permissions model.

IllegalArgumentException in grantUriPermission on API level 19

The following line of code
context.getApplicationContext().grantUriPermission(packageName, uri, Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
raises this exception when running on devices with API level 19 (KitKat), but not on later versions:
java.lang.IllegalArgumentException: Requested flags 0x40, but only 0x3 are allowed
at android.os.Parcel.readException(Parcel.java:1476)
at android.os.Parcel.readException(Parcel.java:1426)
at android.app.ActivityManagerProxy.grantUriPermission(ActivityManagerNative.java:3461)
at android.app.ContextImpl.grantUriPermission(ContextImpl.java:1732)
at android.content.ContextWrapper.grantUriPermission(ContextWrapper.java:577)
Why is that so?
I believe this is caused by a change added in KitKat which should have fixed content access but they broke it.
You would need to run a check using Build.VERSION.SDK_INT < 19 (ie. pre-KitKat)
if(Build.VERSION.SDK_INT < 19) {
context.getApplicationContext().grantUriPermission(packageName, uri, Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
} else {
takePersistableUriPermission(packageName, uri);
}
http://developer.android.com/reference/android/content/ContentResolver.html#takePersistableUriPermission
I think this is a bug in KitKat.
https://android.googlesource.com/platform/frameworks/base/+/kitkat-mr2.2-release/services/java/com/android/server/am/ActivityManagerService.java#6214
Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION is missing in check condition.
from lolipop version, it works correctly

Categories

Resources