How to get value of app's location permission on Phonegap Android - android

I need to find out if location permission for current app is disabled or not.
I can use cordova.plugins.diagnostic.isLocationEnabled to check if global phone's location services are turned off or on, but that doesn't take into account permissions settings for current app.
I tried using navigator.geolocation.getCurrentPosition but it never fails if i disable location permission, it just times out giving me error code 3 which is timeout error. But I need to differentiate between request timeout and location permission being turned off

Ok I found out the answer. This is Android 6 specific issue. It has 2 levels of location settings. Global and app's specific. cordova.plugins.diagnostic will add support for app's specific permission settings soon.
https://github.com/dpa99c/cordova-diagnostic-plugin/issues/11

Related

Ionic - GPS enabled

I'm looking for a simple way to know the GPS permission status on an iOS/Android device.
I'll give an example to clarify my question:
If the GPS on the device is turned off (so there is no way to obtain the longitude/latitude coordinates) I would display a label in this way.
<p *ngIf="!isGPSTurnedOn()">GPS is turned off, please go to settings and turn it on!<\p>
And in the ts file:
isGPSTurnedOn() { return .... }
There is also the possibility that the GPS is turned on but the location permission is not granted by the user. Something like this
<p *ngIf="!isGPSPermissionGranted()">Location permission is not granted. Please go to settings and grant it!<\p>
Same story in the ts file.
My final goal is to try to obtain GPS position if both permissions are granted: otherwise, the user will be warn about the issue.
Thanks in advance!
What you need is the Ionic Native - Diagnostic plugin.
https://ionicframework.com/docs/native/diagnostic/
With this plugin, you can check whether device features are enabled or not, GPS included.
Some useful methods (from plugin documentation):
isLocationEnabled() :
Returns true if the device setting for location is on. On Android this returns true if Location Mode is switched on. On iOS this returns true if Location Services is switched on.
isLocationAuthorized() :
Checks if the application is authorized to use location. Note for Android: this is intended for Android 6 / API 23 and above. Calling on Android 5 / API 22 and below will always return GRANTED status as permissions are already granted at installation time.
getLocationAuthorizationStatus() :
Returns the location authorization status for the application.
requestLocationAuthorization(mode) :
Returns the location authorization status for the application. Note for Android: this is intended for Android 6 / API 23 and above. Calling on Android 5 / API 22 and below will always return GRANTED status as permissions are already granted at installation time.

Set preferred network type

I want Android source code to set preferred network type from my android application.
I can read preferred network by following code.
Settings.Global.getString(context.getContentResolver(),Settings.Global.NETWORK_PREFERENCE)
Because of user privacy and security, apps are not allowed to change the preferred network type
check out this answer: Change mobile network mode (gsm, wcdma, auto)
When you'll scroll the above link, you'll find this answer. But when you'll go to your AndroidStudio and start adding the permissions to your AndroidManifest.xml file, you'll come across this :
Which says that the permission is granted only to the system apps.

Android 6.0.1 - App details vs App permissions details vary

I have LeEco2. I am working on an app and I see something strange.
In my App permissions in Location, I see that location has been disabled for app but in Location->Location Access, it shows Allowed
In App Management, I see Permissions has no access to location
Location->Location Access in the settings is to enable the GPS and the Permissions in your app settings is Permission for your app to use the Location Service.
You can have the following.
You can request for Location Permission and It can be given while the GPS is still turned off.
LOCATION SERVICE could be on and your app could have got the permission but then user can go back and turn off either of them.
I suggest you always check if GPS is ON before triggering location permission and before using it as well.

Beacon detection stops when giving ACCESS_FINE_LOCATION permission in Marshmallow

I have set beacon background scan using this tutorial in BaseApplication class but in Marshmallow running device it shows this log:
Caught a RuntimeException from the binder stub implementation.
java.lang.SecurityException: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to get scan results
And finally with this and this reference i was able to give location access for Marshmallow running device to detect beacons.
My Problem:
Even when i give Location access it doesn't detect beacons and also stops to show above Log. Is it the problem as in this ISSUE. My Nexus 5 Build number is MRA58N
UPDATE: When i turn on Location manually now it works. But it's strange. Is it right way to detect beacon?
Android Marshmallow introduces an entirely new spin on application permissions,Users now have the ability to revoke runtime permissions whenever they desire. This means that you can’t assume the app has access to the permission, even if it had been granted previously. You can refer this lib or this guide. And you can create a interface listener location changed after enable GPS, when location != 0. After enable GPS you must resume. I Hope this will help you out.

Android - turn GPS on or off programmatically

Why we need to go setting for on/off GPS on the other hand we can on/off WIFI and Bluetooth programmatically without move to settings.
Android Guidelines have changed above version 4.0. You cannot change GPS off on programmatically for versions above 4.0.
There used to be a way to enable / disable GPS programmatically by sending the android.location.GPS_ENABLED_CHANGE broadcast:
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", enabled);
sendBroadcast(intent);
where enabled would be true or false respectively.
If you take a look at this bug report, this hack was subverted in Android 4.4. It still works on older OS versions.
Now the answer to your question
Why we need to go setting for on/off GPS on the other hand we can
on/off WIFI and Bluetooth programmatically without move to settings ?
Android's GPS technology periodically sends location data to Google even when no third-party apps are actually using the GPS function. A lot of people are very sensitive about things like real-time location monitoring . That's why Google made it mandatory to get the user's consent before using the GPS function. The following dialog is seen whenever the user turns GPS on:
And hence it is no longer possible to programmatically change the GPS settings, as by necessity it requires the user's permission. What the programmer can do is direct the user to the GPS settings by calling
startActivity(context, new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
and let the user make a choice.
As an interesting point, if you try sending the GPS_ENABLED_CHANGE broadcast on the new OS versions, you get a
java.lang.SecurityException: Permission Denial:
not allowed to send broadcast android.location.GPS_ENABLED_CHANGE
error. As you can see, its a SecurityException with a permission denial message.
The premise of your question is no longer correct. With Google Play Services 7, you can display a dialog to change the location provider settings from within your app. Jump to 1:10 in this video.

Categories

Resources