Android - turn GPS on or off programmatically - android

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.

Related

Can I force android to recheck connection if it's lost?

Can I from my application force android to reconnect to cellular network immediately?
If yes how to do this?
Apparently the functionality as of Android 5 has been moved to system apps only. How to enable mobile data on/off programmatically
But you can still register a listener for network changes.
https://developer.android.com/training/basics/network-ops/reading-network-state
When you get the onLost() event you can start a Wireless Setting Activity so the user can do it manually.
//As seen in the link above.
Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
startActivity(intent);
This will be a common occurrence so get use to it. For example you can createBond() for a Bluetooth device, but you can't removeBond() anymore. Thus you have to send the user to the Bluetooth setting activity for the user to
"forget" about the device.

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.

Plays SettingsAPI - Is there way to request only GPS turned on or only WIFI Turned On?

During resolution, SettingAPI will present dialog, but it contains everything that it detects the user needs to activate. For example, WIFI and GPS. I would like to prompt the user to turn on only GPS for example, but not WIFI for security reasons. If I send them to settings instead of the using Plays ,new Settings API they have the option of turning everything on. I am trying to limit to just one type of setting and I would like to use the SettingAPI dialog for this.
The best bet here is not to add the
android.permission.ACCESS_WIFI_STATE permission.
This way the app won't have any information about Wi-Fi Networks.
But do add the
android.permission.ACCESS_COARSE_LOCATION and android.permission.ACCESS_FINE_LOCATION in the Manifest file so that the app can always look for the GPS.
Hope this Helps!!
SettingsApi doesn't allow you to specify location providers directly (GPS location, network location provider, etc). Instead, you can only ask for location priorities, and SettingsApi chooses location providers for you.
As there's no GPS-only location priority, you can't request that you only need to turn on GPS, but leave network location off. You can request for network-only location mode by setting your location priority to either PRIORITY_BALANCED_POWER_ACCURACY or PRIORITY_LOW_POWER.

Android: How to set GPS always ON with root and shell, to track workers

I'm writing small app for our workers, to work in terrain, and I have some idea. We want to track their location for better logistics and better work control.
App is not a problem for me, but button to turning on/off GPS/Locations Manager. Is there any possibility to set GPS always ON, and unable to turn OFF, or maybe something that will detect turning OFF, and turn GPS back ON every few seconds (?bash script?).
I've rooted my device (GALAXY GIO) and I have full access to CLI. I know how to disable/enable almost every device, but GPS is still puzzle for me...
Is it possible to do something like that without changing ROM? Current ROM is 2.3.6 Gingerbread.
Thanks in advance!, Mike
Use services and place this code in your services.
String provider = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (!provider.contains("gps")) {
// if gps is disabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings","com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
/*
* Toast.makeText(getApplicationContext(), "GPS On Success",
* Toast.LENGTH_SHORT).show();
*/
}
There's no reliable way of doing what you need, other than exploits (which looks like only work for 2.2 and some 2.3 builds) and custom ROMs (If you can find one that does this).
In Android, the user has control over most functions. Android is aimed for personal use, and it's not very well suited for this kind things. You migh be better off switching to another platform (e.g.: BlackBerry) that allowed you to do this, or even reading data via bluetooth from a GPS tracking device that is turned on always. They can still disable bluetooth, so you migh need to search for a fully autonomous GPS tracking device (there are many available in the market, some of them also have a SIM card so they can send data in real time).
As a final note, I'd say most workers will gladly enable the GPS if they are required to do so, and in case they disable it, you can still detect it and report to a WS or log, in order to ask them for an explanation later.

Categories

Resources