Android background location request permissions not allowed help api 28 and lower - android

I want location permissions for android api 28 and below.. It takes foreground location permissions but not background permissions.. Also on android developers page it says "background location" permissions will be granted automatically when Foreground permissions are taken for below 28, but it does not "grant" "background location" permissions I couldn't solve this problem, can anyone help?
source:https://developer.android.com/training/location/permissions#background
details:
Blockquote
On Android 10 (API level 29) and higher, you must declare the ACCESS_BACKGROUND_LOCATION permission in your app's manifest in order to request background location access at runtime. On earlier versions of Android, when your app receives foreground location access, it automatically receives background location access as well.
Blockquote
//Create location request permissions
var requestLocationPermission=registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()){
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
if (it.getOrDefault(Manifest.permission.ACCESS_COARSE_LOCATION,false) &&
it.getOrDefault(Manifest.permission.ACCESS_FINE_LOCATION,false)){
println("Permissions : Granted")
if (ContextCompat.checkSelfPermission(requireContext(),Manifest.permission.ACCESS_BACKGROUND_LOCATION)==PackageManager.PERMISSION_GRANTED){
println("Background location permissions is granted..!")
}else{
println("Background location permissions is denied..!")
}
}else{
println("Permissions : Denied")
}
}
}
// Code 2
fun requestPermission(){
if (ContextCompat.checkSelfPermission(requireContext(),Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(requireContext(),Manifest.permission.ACCESS_COARSE_LOCATION)==PackageManager.PERMISSION_GRANTED){
println("Permissions granted")
}else{
println("Permissions request")
requestLocationPermission.launch(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION))
}
}

Related

changed targetSdkVersion to 33 from 30 and now notifications are not coming up

I updated targetSdkVersion from 30 to 33 and notifications popup is not shown when the app is installed on the device
when the targetSdkVersion is 30 and when I install the app, the following popup shows up and when I click allow I do get notifications
when the targetSdkVersion is 33, I donot get the following popup when the app is installed.
I looked at https://developer.android.com/develop/ui/views/notifications/notification-permission
and added <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/> in manifest file.
still I do not get any prompt asking for permission for notifications.
I have code which checks if notification is enabled and it returns null cuz no prompt is shown.
private fun isNotificationsEnabled(): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val manager = oApp.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (!manager.areNotificationsEnabled()) {
return false
}
val channels = manager.notificationChannels
for (channel in channels) {
if (channel.importance == NotificationManager.IMPORTANCE_NONE) {
return false
}
}
true
} else {
NotificationManagerCompat.from(oApp).areNotificationsEnabled()
}
}
How can i force prompt or what else I need to do so user get this prompt on first install
thanks in advance
R
As shown in the documentation here, the new POST_NOTIFICATIONS permission is classified as a dangerous permission, which means you must manually request the permission from the user at runtime. The process for requesting a dangerous permission from the user is explained in the documentation here. Best practices for dangerous permissions state that you should not request the permission until the point when the user requests the behavior that requires the permission.

Android 12 ACCESS_FINE_LOCATION returned as PERMISSION_DENIED even though it's GRANTED

On Android 12 devices (API 31),
User has GRANTED both the precise and approximate permissions and app is working fine post that.
When user turns off the device location by going into settings, I see the ACCESS_FINE_LOCATION permission as NOT GRANTED.
When checked by navigating into settings, we see precise location is turned on and location is set to allow all the time. Still the ACCESS_FINE_LOCATION is returned as NOT GRANTED.
In this scenario below code returns false -
override fun hasLocationPermission(): Boolean {
return PermissionChecker.checkSelfPermission(
mContext,
android.Manifest.permission.ACCESS_FINE_LOCATION
) === PermissionChecker.PERMISSION_GRANTED
}
So if user turns off device location then FORGROUND LOCATION PERMISSION IS NOT GRANTED. is this the expected behaviour in Android 12?
Please assist.
I think that PermissionChecker returns PERMISSION_DENIED_APP_OP. PermissionChecker can return three result: PERMISSION_DENIED, PERMISSION_GRANTED or PERMISSION_DENIED_APP_OP. If you want result PERMISSION_GRANTED, try to use ContextCompat.
override fun hasLocationPermission(): Boolean {
return ContextCompat.checkSelfPermission(
mContext,
android.Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED }

How do I request push notification permissions for android 13?

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);
}

Android 12 not showing correct permissions request screen

My app requires precise user location because it requires the user to be at specific locations to perform tasks. I have followed the documentation for the proper way to request both ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION together.
class LocationChooserFragment : BottomSheetDialogFragment() {
// only relevant parts shown
/**
* A utility class that holds all the code for requesting location updates,
* so that we can re-use it in multiple fragments.
*/
private var locationTracker: LocationTracker? = null
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
// all the other initialization for viewbinding
tryToGetLocationUpdates()
}
private fun tryToGetLocationUpdates() {
val request = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
if (permissions[Manifest.permission.ACCESS_FINE_LOCATION] == true && permissions[Manifest.permission.ACCESS_FINE_LOCATION] == true) {
requestLocationUpdates()
}
}
when {
ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED -> {
Timber.i("Already have required permissions")
requestLocationUpdates()
}
shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION) ||
shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_COARSE_LOCATION) -> {
Timber.i("Showing permission rationale dialog")
// A wrapper to show a dialog that explains what we need permissions for
alertDialog(
title = getString(R.string.permissions),
msg = getString(R.string.why_we_need_location),
onClick = { yes ->
if (yes) {
request.launch(arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
))
}
}
)
}
else -> {
Timber.i("Prompting for location permissions")
request.launch(arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
))
}
}
}
private fun requestLocationUpdates() {
if (locationProvider == null) {
locationProvider = LocationProvider(requireContext())
}
locationProvider?.requestLocationUpdates()
}
}
The problem I have is that my users are not getting prompted to allow precise location.
I expect to see the prompt in Figure 3 from the documentation:
Figure 3. System permissions dialog that appears when your app requests both ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION in a single runtime request.
Instead, I see the prompt in Figure 2 from the documentation:
Figure 2. System permissions dialog that appears when your app requests ACCESS_COARSE_LOCATION only.
Additionally, I expect that when I already have COARSE permission and I request FINE and COARSE permission again, I should see the "Upgrade to Precise permission" dialog, but I don't see any prompt at all.
I have tested this on Pixel 3a (physical device and emulator) as well as received reports from other users on various Pixel and Samsung Galaxy devices that are running Android 12.
The problem was not present when we used compileSdk 30 and targetSdkVersion 30 in build.gradle, but we need to be able to support new features and new versions of Android (and to be able to upgrade to dependencies that only support building with newer SDK versions.
Why is the permission dialog not showing properly?
Comparing my AndroidManifest.xml to the generated file in the built app (using the "Analyze APK" feature in Android Studio), I discovered that the generated APK file had the following entry:
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"
android:maxSdkVersion="30" />
Clearly, the maxSdkVersion is preventing the OS from giving me the permission I need.
Using grep on the app\build directory, I found app\build\intermediates\manifest_merge_blame_file\appnameDebug\manifest-merger-blame-appname-debug-report.txt, which included these lines:
17 <uses-permission
17-->C:\path\to\my\appname\app\src\main\AndroidManifest.xml:12:2-76
18 android:name="android.permission.ACCESS_FINE_LOCATION"
18-->C:\path\to\my\appname\app\src\main\AndroidManifest.xml:12:19-73
19 android:maxSdkVersion="30" />
19-->[com.github.50ButtonsEach:flic2lib-android:1.3.1] C:\Users\moshe\.gradle\caches\transforms-3\dced3886509296f1029e8245af379a07\transformed\jetified-flic2lib-android-1.3.1\AndroidManifest.xml:17:9-35
It turns out the developers of this library originally used ACCESS_FINE_LOCATION for connecting to nearby Bluetooth devices, and since Android 12 has separate permissions for that, they don't need it anymore so they added a max SDK version.
I added the tools:remove="android:maxSdkVersion" property to the ACCESS_FINE_LOCATION <uses-permission /> element, and now my permissions work properly.

Android 6.0 bug ? Have permission but getScanResults() still return empty list in Android 6.0

I have request the permission in android version 6.0 - Marshmallow,But it still return empty list when using getScanResults().
private boolean checkPermission() {
List<String> permissionsList = new ArrayList<String>();
if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
permissionsList.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
permissionsList.add(Manifest.permission.ACCESS_COARSE_LOCATION);
}
if (permissionsList.size() > 0) {
ActivityCompat.requestPermissions((Activity) mContext, permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
return false;
}
return true;
}
After request permission, then in the onRequestPermissionsResult method,I have get the permission of ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION, But I still can not the scan result
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS:
if (permissions.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED ||
(permissions.length == 2 && grantResults[0] == PackageManager.PERMISSION_GRANTED &&
grantResults[1] == PackageManager.PERMISSION_GRANTED)){
List<ScanResult> scanResults = mWifi.getScanResults();
//list is still empty
}
else {
// Permission Denied
Toast.makeText(mContext, getString(R.string.permission_deny), Toast.LENGTH_LONG).show();
}
break;
}
}
Is this a bug of android M?
You still need to enable WIFI after you request the permission. So in short, you have to do this in sequence for scanning the perimeter:
Request the necessary permissions (ACCESS_WIFI_STATE, CHANGE_WIFI_STATE, ACCESS_COARSE_LOCATION). Additionally, on MM you need to request this at run-time, as you stated.
Enable WIFI with WifiManager#setWifiEnabled(true);
You don't have to enable location access programatically that I know off. But read the note below.
You have to register a BrodcastReceiver for SCAN_RESULTS_AVAILABLE_ACTION. This is where you get the signal that the scans are ready. Doesn't matter if you register through AndroidManifest or dynamically at run-time, as long as it's done before the next step.
You have to WifiManager#startScan() in order to request exactly ONE update for network scans. If you want more, set up a timer/timertask (recommended) or reschedule when you receive the previous one (which might never come)
Only on the BroadcastReceiver onReceive will you be able to call WifiManager#getScanResults() with plausible results.
Note: On some phones (Moto X 2014), I noticed you need basic location enabled to get any results, which only the user (system UI) seems to be able to do trigger on/off. If the user has location completely off, I can't seem to get a non-empty result list, even though the system UI can. This is likely due to Marshmallow needs to have location for Bluetooth and WiFi scans in user apps, and a bad implementation by Motorola, or a defect already fixed in latest Marshmallow bug tracker but not in Motorola's latest OTA, because this doesn't happen in a Nexus 5 or a Galaxy S6.
on nexus 5, with M update, it appears to me I also need to have GPS location turned on to get this working.
Maybe it cause by android M's runtime permission management,you can set targetSdkVersion 19(less than 21 or 23) to try again. It works for me
In Android Marshmallow 6.0.1 go to Settings > Connections and hold down Location. There click on Location method and you will see three options:
High accuracy: uses GPS, Wi-Fi and mobile networks.
Battery saving: uses Wi-Fi and mobile networks.
Device only: uses GPS.
Select Battery saving in order to get Wi-Fi scan results without enabling GPS.
The permission is requested when needed at run-time. Note that it is NOT:
Settings -> Location
It is checking:
Settings -> Applications -> (YOUR APP) -> Permissions
That is where the more granular run-time permission must be set. It must be set for each application as of version 6.

Categories

Resources