I have recently updated the code for an Android app to request permissions on Android 6.0+.
However, I am now facing a dilemma on how I want to check for permissions.
I saw people online checking the OS version before checking for permissions, because versions before 23, permissions are unnecessary to check due to them being granted on installation.
Right now my checks look like this,
if(checkPermissions()){
doThings();
} else {
requestPermissions();
}
but should I take the effort to add this?
if (Build.VERSION.SDK_INT >= 23) {
if(checkPermissions()){
doThings();
} else {
requestPermissions();
}
} else {
doThings();
}
I don't see the point of adding the latter to the code as from my understanding, older versions of android could run into the first sample code just fine.
All this brings me to ask, is there a benefit to checking Android version in this case?
but should I take the effort to add this?
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(checkPermissions()){
doThings();
} else {
requestPermissions();
}
} else {
doThings();
}
You should if checkPermissions(), and requestPermissions() invokes classes or methods strictly API 23 or later. Or else, you may do as you wish.
All this brings me to ask, is there a benefit to checking Android version in this case?
As far as I know, the verification by itself isn't necessary, given what I said above. So this check is just an expendable if clause cluttering your code.
ContextCompact.checkSelfPermission
return always PERMISSION_GRANTED in android versions < 23.
So you can avoid check build version.
More info here
Related
What permission do we need to use instead of WRITE_EXTERNAL_STORAGE in Android 13 app on Android 13 device? I have read through the documentation but not able to get what permission we need to use. The function which needs this is a photo picker which access the images, before it used to ask for permission on Android 12, but once the app was upgraded to Android 13 and used on an Android 13 app, the photo picker stopped working. It uses the permission WRITE_EXTERNAL_STORAGE which is not required anymore. But what needs to be used instead of this?
The function is from a library - react-native-image-crop-picker
#ReactMethod
public void openPicker(final ReadableMap options, final Promise promise) {
final Activity activity = getCurrentActivity();
if (activity == null) {
promise.reject(E_ACTIVITY_DOES_NOT_EXIST, "Activity doesn't exist");
return;
}
setConfiguration(options);
resultCollector.setup(promise, multiple);
initiatePicker(activity);
permissionsCheck(activity, promise, Collections.singletonList(Manifest.permission.WRITE_EXTERNAL_STORAGE), new Callable<Void>() {
#Override
public Void call() {
return null;
}
});
}
Use react-native-permissions for managing the permissions.
The docs are quite simple, kindly go through this.
Some permission changes were required for this in the react-native-image-cropper library - https://github.com/ivpusic/react-native-image-crop-picker/pull/1852
I've looked through this guide for android 13 push notifications
https://developer.android.com/about/versions/13/changes/notification-permission#user-choice
And I've looked at the guide for requesting permissions
https://developer.android.com/training/permissions/requesting#java
I've updated my compile and target to api 32.
Here is my code so far (in progress). Right now I'm just trying to get the notification prompt to show up.
if (Build.VERSION.SDK_INT >= 32) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_NOTIFICATION_POLICY) == PackageManager.PERMISSION_GRANTED)
return;
ActivityResultLauncher<String> launcher = registerForActivityResult(
new ActivityResultContracts.RequestPermission(), isGranted -> {
}
);
launcher.launch(Manifest.permission.POST_NOTIFICATIONS);
}
The problem I have is I get an error cannot find symbol variable POST_NOTIFICATIONS.
What is the proper manifest permission for push notifications?
Maybe I'm a bit late to the party, I know... But I hope this can help others at least. You need to use compileSdkVersion 33 in your gradle file at the Module level. Then you'll be allowed to use the POST_NOTIFICATIONS permission without any issue.
Android 13 (API level 33) and higher supports a runtime permission for sending non-exempt (including Foreground Services (FGS)) notifications from an app: POST_NOTIFICATIONS. This change helps users focus on the notifications that are most important to them.
reference here
So you need to add this permission to AndroidManifest.xml
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
You need to follow few steps, add post notifications permission in manifest
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
then in your controller as for run time permission like generally we ask:
if (Build.VERSION.SDK_INT >= 33) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.POST_NOTIFICATIONS},101);
}
else {
createChannel();
}
}
Then Need to handle permission result as usual
How to add run time permission for notification permission in android studio,
Apps targeting Android 13 will now need to request notification permission from the user before posting notifications,”
Behavior changes: Apps targeting Android 13 or higher
https://developer.android.com/about/versions/13/behavior-changes-13
Add on manifest: uses-permission android: name="android.permission.POST_NOTIFICATIONS
and
Add in MainActivity after onCreate:
if (ContextCompat.checkSelfPermission(this, POST_NOTIFICATIONS) == PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(this, new String[]{POST_NOTIFICATIONS}, 1);
}
I really struggle with this since a while :( as I need an solution that works within UNITY3D.
I need to check if the user has given the permission to access the Android device camera (and location on a second level).
Normally the app start by asking for this permissions at launch, but if the user denies the access for the camera I need to know and check that later.
Otherwise the user could hit the camera UI button I made and try to access the camera via webcamtexture... and that leads into a crash of the app.
Since Android API 23 you cannot ignore or already grant permissions by changing the android manifest like I tried after reading several posts about that.
Thank's to everyone who has an idea to solve this.
Check this library: https://github.com/sanukin39/UniAndroidPermission
In that library I got these methods to check and request Permission.
public static void requestPermission(String permissionStr){
if(!hasPermission(permissionStr)) {
UnityPlayer.currentActivity.requestPermissions(new String[]{permissionStr}, 0);
}
}
public static boolean hasPermission(String permissionStr) {
if(Build.VERSION.SDK_INT < 23) {
return true;
}
Context context = UnityPlayer.currentActivity.getApplicationContext();
return context.checkCallingOrSelfPermission(permissionStr) == PackageManager.PERMISSION_GRANTED;
}
Hope it helps:)
WebChromeClient supply "onShowFileChooser" on Android 5.0, also supply "openFileChooser" method below Android 4.4 method,How to compatible with them? Has anyone managed to do this?
Maybe you can use "Build.VERSION.SDK_INIT" to check the current sdk version on the phone used your app. And decide which to use.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// do something with onShowFileChooser()
} else {
//do something with openFileChooser()
}
I'm not new to Android and I'm well used to the version handling and how to condition it, but when I see this it troubles me...
// Check if we're running on Android 5.0 or higher
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Call some material design APIs here
} else {
// Implement this feature without material design
}
On any device pre lollipop this line would crash the app because the Build.VERSION_CODES.LOLLIPOP field does not exists... so why is this in the recommended solution in the documentation?
I'm really wondering what am I missing?
Well in that case use this
// Check if we're running on Android 5.0 or higher
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Call some material design APIs here
} else {
// Implement this feature without material design
}
Build.VERSION_CODES.LOLLIPOP = 21
Well, you must compile your project with the latest SDK version. Your constants are replaced with corresponding integer values during compilation. No matter what version of Android you run the application on - integers are the same
Try this one
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
// Marshmallow+
}else{
//below Marshmallow
}
Note: Build.VERSION_CODES.LOLLIPOP_MR1==22
Build.VERSION_CODES.M==23
Bit a late to answer, but, today I encountered with the same issue on Android Studio 3.4.1
So the workaround is:
Upgrade to the latest Android SDK.
And,
After Marshmallow/Android 6 Build.VERSION_CODES.xxx full names are replaced with initials and some other variations.
So, now for Marshmallow, it will be:
Build.VERSION_CODES.M
And for Nougat:
Build.VERSION_CODES.N
And so on.
Read more about the build version codes here: Android Developer Reference