POST_NOTIFICATIONS permission dialog not showing (Android 13, targetSdkVersion=33) - java

There are many questions on this topic already, I know. However, none of the answers I have found so far resolves the issue for me.
The problem is ActivityCompat.requestPermissions does not trigger a popup, asking the user to give permission for notifications.
Setup:
Testing on physical device (Android 13, Samsung Galaxy S22 and Samsung Galaxy A51)
targetSdkVersion = 33
compileSdkVersion = 33
minSdkVersion = 23
AndroidManifest has permission: <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
Activity declared in AndroidManifest
<activity android:name=".PostNotificationActivity"
android:exported="true"
android:screenOrientation="portrait"/>
Click on Enable Notification button:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.POST_NOTIFICATIONS)) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.POST_NOTIFICATIONS),
POST_NOTIFICATION_PERMISSION_CODE)
} else {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.POST_NOTIFICATIONS),
POST_NOTIFICATION_PERMISSION_CODE)
}
}
and the callback is like so:
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when(requestCode) {
POST_NOTIFICATION_PERMISSION_CODE -> {
if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(applicationContext, "Permission Granted", Toast.LENGTH_LONG).show()
} else {
Toast.makeText(applicationContext, "Permission Denied", Toast.LENGTH_LONG).show()
}
return
}
}
}
I have stepped through the code via the debugger, and can confirm that on first button click requestPermissions is called but no popup on device and onRequestPermissionsResult is also not trigger as well.
And when again clicked the button requestPermissions is called with no popup on device but onRequestPermissionsResult is triggered now with Permission Denied and when press back previous activity is freezed.
I have also tried using the new ActivityResultLauncher, but the behaviour is same no popup on device.

Related

Storage Permission Issue In android 10+ devices

i am trying to create a video player ,So I am trying to add the videos to the list
Storage permission is required to fetch the videos, so I took the permission with the below code.
But playstore was reject My app for this MANAGE EXTERNAL STORAGE permission.
But without this permission, I can't get storage permission on Android 10+ device.
To change the name of the video, delete the video and download the video permission is required , so please help me , please tell me how to get storage permission (/storage/Media/Videos , /storage/Download/)
My storage permission code :-
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
android:requestLegacyExternalStorage="true"
Main activity code :-
private boolean checkPermission() {
if (SDK_INT >= Build.VERSION_CODES.R) {
return Environment.isExternalStorageManager();
} else {
int result = ContextCompat.checkSelfPermission(PermissionActivity.this, READ_EXTERNAL_STORAGE);
int result1 = ContextCompat.checkSelfPermission(PermissionActivity.this, WRITE_EXTERNAL_STORAGE);
return result == PackageManager.PERMISSION_GRANTED && result1 == PackageManager.PERMISSION_GRANTED;
}
}
private void requestPermission() {
if (SDK_INT >= Build.VERSION_CODES.R) {
try {
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse(String.format("package:%s",getApplicationContext().getPackageName())));
startActivityForResult(intent, 2296);
} catch (Exception e) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
startActivityForResult(intent, 2296);
}
} else {
//below android 11
ActivityCompat.requestPermissions(PermissionActivity.this, new String[]{WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2296) {
if (SDK_INT >= Build.VERSION_CODES.R) {
if (Environment.isExternalStorageManager()) {
// perform action when allow permission success
} else {
Toast.makeText(this, "Allow permission for storage access!", Toast.LENGTH_SHORT).show();
}
}
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0) {
boolean READ_EXTERNAL_STORAGE = grantResults[0] == PackageManager.PERMISSION_GRANTED;
boolean WRITE_EXTERNAL_STORAGE = grantResults[1] == PackageManager.PERMISSION_GRANTED;
if (READ_EXTERNAL_STORAGE && WRITE_EXTERNAL_STORAGE) {
// perform action when allow permission success
} else {
Toast.makeText(this, "Allow permission for storage access!", Toast.LENGTH_SHORT).show();
}
}
break;
}
}
So please tell me how to take storage permission in Android10+ Devices and also below Android 10 devices with out using MANAGE EXTERNAL STORAGE permission , Please Help Me
Permissions have changed in Android 10+:
External storage access scoped to app files and media
By default, apps targeting Android 10 and higher are given scoped access into external storage, or scoped storage. Such apps can see the following types of files within an external storage device without needing to request any storage-related user permissions [..]
Source: Privacy changes in Android 10
On devices that run Android 10 or higher, you don't need any storage-related permissions to access and modify media files that your app owns, including files in the MediaStore.Downloads collection
Source: Storage Permissions
If you have
android:requestLegacyExternalStorage="true"
in application tag in manifest file then you are done for an Android Q/10 device.
It will behave as it behaved on before 10.
I do not understand why you would have any problem on Android 10.
On Android 11+ you should be able to see media files in the usual public directories.

Ask Same Android Studio Permissions Over And Over

I need to access the user's location, and the permission for it is crucial for my app.
I tried this code but it did not ask for permission the second time (and so forward):
while(ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(MainActivity.this, permissions, 1);
}
If the permission is really crucial to continue in the app, as far as I know, the only think you can do is just to finish the app when you get in the permissions callback that this permission has not been granted.
This way the next time the app is opened it will ask again for the location permission.
We check for permissions at the main activity and show a toast that says that the permissions are required and finish the main activity in one app which makes no sense using it without location. And the app is published in the google play store.
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if (permissions.length > 0 && requestCode == REQUEST_PERMISSIONS) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
setUp();
} else {
Toast.makeText(this, R.string.permissions_required, Toast.LENGTH_LONG).show();
finish();
}
}
}
Even though it might be better if the rest of the app could be used without location, but sometimes there is no other way and if you try to ask for the same permission once and again the dialog will not show up at some point.

How to get draw overlays permission from a library of an application in marshmallow

I'm trying to get permission for drawing overlays in marshmallow.
In marshmallow we must give run-time permission for drawing overlays and I have implemented. What issue I'm getting is once I have given the permission from my app next time if I check with following code, if(Settings.canDrawOverlays(getApplicationContext()) it returns false.
But in settings (Draw over other apps) my app is Checked. Here I have provided my code of lib activity.
if(Settings.canDrawOverlays(getApplicationContext())&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
Toast.makeText(getApplicationContext(), "Windows Overlay allowed" , Toast.LENGTH_SHORT).show();
startService(new Intent(this, FABService.class));
finish();
}
else{
startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION), 1);
}
and my onActivityResult is,
#TargetApi(Build.VERSION_CODES.M)
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
/** check if received result code
is equal our requested code for draw permission */
if (requestCode == 1) {
// * if so check once again if we have permission /
if (Settings.canDrawOverlays(getApplicationContext()) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// continue here - permission was granted
Toast.makeText(getApplicationContext(), "Windows Overlay allowed", Toast.LENGTH_LONG).show();
startService(new Intent(this, FABService.class));
finish();
}
}
}
These code I have implemented in lib of that application. Any 1 can help me where I have went wrong.
Put permission in Manifest
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
Use this for Overlay permission
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
// Show alert dialog to the user saying a separate permission is needed
// Launch the settings activity if the user prefers
Intent myIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
startActivity(myIntent);
}

How to set CAMERA permission in order to pass regular test?

It is a very strange situation. I am working with camera2 API and there is a regular method to open the camera.
manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler);
this method is requiring to make test, this one
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) !=
PackageManager.
return;
}
it is simple test to deduce if there was declared CAMERA permission in manifest or not.
<uses-permission android:name="android.permission.CAMERA" />
i have this permission in my manifest file and if i am uploading this app to Samsung S5 it is work properly without problem, but if i am uploading this app to chinese device, mistake is happen. Don't pass the test and eventually don't open the camera...
Maybe should i set permission dynamically?
And one more thing, i have tryed call method to open the camera inside test, this way
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) !=
PackageManager.
manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler);
return;
}
but get this mistake
FATAL EXCEPTION: main Process: com.example.android.camera2basic, PID: 29649 Theme: themes:{} java.lang.SecurityException: Lacking privileges to access camera service at android.hardware.camera2.utils.CameraBinderDecorator.throwOnError(CameraBinderDecorator.java:108) at android.hardware.camera2.legacy.CameraDeviceUserShim.connectBinderShim(CameraDeviceUserShim.java:336) at android.hardware.camera2.CameraManager.openCameraDeviceUserAsync(CameraManager.java:327) at android.hardware.camera2.CameraManager.openCamera(CameraManager.java:457) at com.example.android.camera2basic.activities.CameraActivity.openCamera(CameraActivity.java:919)
What i am doing wrong?
On API level 23+ you need to request permissions at runtime (even if you have them declared in your Manifest).
You should do something like this:
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
CAMERA_REQUEST);
To process the result you need to override onRequestPermissionsResult() in your Activity:
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == CAMERA_REQUEST) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission granted
// you can do your Camera related stuff
} else {
// permission denied
}
}
// ...
}
Check out the documentation on runtime permissions.

When asking for runtime permission for location

Currently when asking for run time permission when launching my app for the very first time it prompts the user to use their location. And if you click yes it doesn't enable location like it should.
But if I relaunch the app it enables the location. Any suggestions as to where I can get it to have location enabled on first launch ? The first part of the code is called in OnCreate.
if (ContextCompat.checkSelfPermission(MapsActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(MapsActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
// 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(MapsActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_CODE_ASK_PERMISSIONS);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == REQUEST_CODE_ASK_PERMISSIONS) {
if (permissions.length == 1 &&
permissions[0] == Manifest.permission.ACCESS_FINE_LOCATION &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
} else {
// Show rationale and request permission.
}
mMap.setMyLocationEnabled(true);
} else {
//permission denied
}
}
}
public void onMapReady(GoogleMap googleMap) {
int permissionCheck = ContextCompat.checkSelfPermission(MapsActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION);
If you are testing on an emulator the permission will only be asked on the first run. How to debug Android 6.0 permissions?
Also if you are not completely uninstalling the app and it is being upgraded from a lower SDK or permissions have been set by the user at runtime and made default, then the runtime permissions will not be asked again.
Try completely uninstalling the app, or checking the default settings and clearing them.
Take a closer look at this line
permissions[0] == Manifest.permission.ACCESS_FINE_LOCATION
permissions is a String array meaning that you should compare using equals()
or compareTo() method.
If this code is in a Fragment, OnRequestPermissionResult() won't be called. So setMyLocationEnabled won't be called until you re open the app.
To fix this change:
ActivityCompat.requestPermissions(MapsActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_CODE_ASK_PERMISSIONS);
To:
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_CODE_ASK_PERMISSIONS);
ActivityCompat.requestPermissions is for Activities. When you use a Fragment you should call requestPermissions directly.

Categories

Resources