I 'm developing a certain app.
I 'm trying to get GPS data in samsung galaxy s8 - android 8.0.
App runs in foreground service, but it doesn't works well.
It returns error string like below.
java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission.
Permissions in Android Manifest file:
"uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" "
"uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" "
Is there any way to get gps location without runtime permission request?
No, there is no way to acquire permissions without asking at the run time. However, from API 22 and lower, all the permissions are automatically granted at install time.
Related
I use the Code A to set the enabled status of WiFi, it works well when I use Android Emulator with both API 26 and API 23 in Android Studio 3.1.3 , and none of prompt window display!
But I get a prompt window "An App is requesting permission to use WLAN. Allow?" in real mobile phone with Android 5.1 when I run the code A.
How can I make the prompt window not to display in real mobile phone? Thanks!
BTW,the real mobile phone is Samsung SM-J5008 with Android 5.1
Prompt Window
I have set the permissions
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
Code A
fun setWiFi(aWiFiDef: WiFiDef): Unit{
val wifiManager =mContext.applicationContext.getSystemService(WIFI_SERVICE) as WifiManager
wifiManager.isWifiEnabled=aWiFiDef.status
}
Android 5.1 does not have such runtime permission, so I assume, that this is a wrongly implemented permission request rational, which is shown even though the app does not need to. You wrote, that you tested with API level 26 and 23, which is Android 6.0 and higher, so these versions already have runtime permissions. Maybe you haven't seen this issue, because you haven't tested on devices below Android 6.0...
Please check if you somewhere call AppCompat.shouldShowRequestPermissionRationale() and if it is surrounded by SDK level checks or at least with a checkSelfPermission as described here: https://developer.android.com/training/permissions/requesting. Test your code in an emulator with Android 5.1 and Android 6.0. Then you should be able to reproduce that issue also on real phones.
You also need to add this to your manifest:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
According to the website below: *App on Marshmallow needs ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission and Location services should be enabled to get wifi scan results. Google probably sees scanning for wifi as a security concern, and wanted to add the location permissions as well.
I would recommend following the blog post below if you are having trouble after adding the ACCESS_COARSE_LOCATION permission to your manifest.
https://www.intentfilter.com/2016/08/programatically-connecting-to-wifi.html
EDIT
For the people down voting, please include a comment explaining why. Was my answer flat out wrong? Did I overlook somthing?
Some more information on google's geolocation service:
Google maintains a huge database for its geolocation service ("Access coarse location" permission). It consists of:
Wifi access point mappings: Wifi MAC address resolve to a geolocation
Cell tower mapping: Cell tower ID's resolve to a geolocation
So it is very possible that this user didn't include this permission and is getting this dialog box.
While developing an app where I scan the WiFi, I found that it does not work if I turn off the location service on my phone. I have provided the app with all the necessary permissions. - ACCESS_NETWORK_STATE, ACCESS_WIFI_STATE, ACCESS_COARSE_LOCATION.
This is my code:
WifiManager manager= (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
manager.startScan();
In the receiver:
int found = manager.getScanResults().size();
This question answers part of the problem.
Wifi scan results broadcast receiver not working
My questions are:
Is there a way for the app to list the Wifi access points if the location service is turned off?
If location service is absolutely necessary, is there a way for the app to turn on the location service while the app scans the wifi access points?
The only way to get the scanResult without GPS turned on is to set the app's targetSDKversion to 21 or lower.
This will work even on Lolipop and above.
Android 8.0 and Android 8.1:
A successful call to WifiManager.getScanResults() requires any one of the following permissions:
ACCESS_FINE_LOCATION
ACCESS_COARSE_LOCATION
CHANGE_WIFI_STATE
If the calling app does not have any of these permissions, the call fails with a SecurityException.
Android 9 and later:
A successful call to WifiManager.startScan() requires all of the following conditions to be met:
Your app has the ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission.
Your app has the CHANGE_WIFI_STATE permission.
Location services are enabled on the device (under Settings > Location).
To successfully call WifiManager.getScanResults() ensure all of the following conditions are met:
Your app has the ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission.
Your app has the ACCESS_WIFI_STATE permission.
Location services are enabled on the device (under Settings > Location).
If the calling app doesn't meet all of these requirements, the call fails with a SecurityException.
This is from https://developer.android.com/guide/topics/connectivity/wifi-scan Google Documentation.
Probably they needed the "Location services are enabled on the device" requirement for Android 6.+ because it is the version this permission restrictions first revealed, but they don't seem to had this requirement in documentations since no one has answered this question until now.
Is there a way for the app to list the Wifi access points if the
location service is turned off?
Yes, only system apps can get scan results without the location with the following permission:
android.permission.PEERS_MAC_ADDRESS
permission
I have an Android Cordova app and I'm using GPS, check the network state, read/write on the Documents folder and taking camera pictures. Here my permissions on the manifest XML file:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
The manifest file is auto generated by the Cordova framework. For some reason I don't see camera permissions. Permissions are not asked at installation time anymore (that's since Android 6) but instead they should be asked before usage.
I correctly get the GPS access permission popup but not the read/write Documents folder permission. I also never get the camera permission albeit I'm able to use it without ever being asked for permission. Same story for the Network status permissions (never being asked).
I find Android permissions scheme extremely confusing, under application manager my app has got Location and Storage as expected, Camera and Network status are missing though.
To recap, inside the app, on the actual code, I'm using at least once those devices
GPS fine grained
GPS coarse (probably the Wifi SSID triangulation trick)
Write on Documents
Read on Documents
Read network status (Offline / Wifi / 3G etc..)
Take picture from the camera
Cordova framework wrote this manifest file:
android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_FINE_LOCATION
android.hardware.location.gps (why is it not a .permission?)
android.permission.ACCESS_NETWORK_STATE
android.permission.WRITE_EXTERNAL_STORAGE
On application manager I get those options:
Location
Storage
So basically I get three different sets of permissions :-(
I found this in the this cordova plugin that you mentioned in comments.
So somehow this <uses-permission android:name="android.permission.CAMERA/> have to be in your code so as this plugin can use it.
Maybe you didn't check the right program to see its permissions, or if the camera permission is not shown in application permissions on device, you can't be able to use camera in this application. Please do a check again because I really want to know what is the situation.
So I have camera permission in manifest , Still when app goes to start camera it crashes .this happens beacuse user had denied the permission in permission manager for camera that comes with xiaomi devices
So the app Crashes , can someone help about how to handle this.
with the normal way of getting permisions , it does not give correct result
String permission = "android.permission.CAMERA";
int res = getContext().checkCallingOrSelfPermission(permission);
res is always 0(has Permission) for below 23 devices , if user has manually denied permission by going to permission manager then also
Revoking permissions on android devices below 23 is non-standard behavior and is afaik only possible through customized OS versions (like Cyanogen mod or in your case, the Xiaomi modified version). Users should be aware, that revoking permissions that way may cause error ins apps.
Prior to Android 6.0, you could reasonably assume that if your app is running at all, it has all the permissions it declares in the app manifest.
https://developer.android.com/training/permissions/best-practices.html#testing
Therefore I suggest you run your methods that require a certain permission with a try/catch. If the api lvl is below 23 and your method call fails, you know for sure if you have the permission or not.
In my Android application I'm willing to use GPS locations.
What are the main permissions that I should included in android
manifest file in order to use GPS locations.
In case of lost the GPS signal strength, is there any way to triangulate the position using mobile networks.
Thank you!
The main permissions you need are android.permission.ACCESS_COARSE_LOCATION or android.permission.ACCESS_FINE_LOCATION.
Only fine location will allow you access to gps data, and allows you access to everything else coarse location gives. You can use the methods of the LocationManager to acquire location data from gps and cell tower sources already, you do not have to work out this information yourself.
If you are targeting API Level 21 (5.0) or higher, you may also need this:
<uses-feature android:name="android.hardware.location.gps" />
This permission should allow your app to use location services through the devices GPS, wifi, and cell towers. Just plop it in your manifest wherever you put your permissions, and it should do the trick. You can find all the other permissions here: (http://developer.android.com/reference/android/Manifest.permission.html)
Here is the code:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
I had the same issue and noticed that the code was requesting high accuracy but the manifest had course permissions. I changed it to fine and the error no longer occurs.
Make sure the code and the manifest reflect the same level of accuracy requested and allowed.
So if the manifest has android.permission.ACCESS_FINE_LOCATION the application can request geolocation.getCurrentLocation({ desiredAccuracy: Accuracy.high...