How to enable location on Android in program code? - android

I would like to enable the location settings on an android machine. In my program I check if the
LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
..//
}
If deactive, I would like to activate it programatically.
EDIT: When my location settings are disabled and I call/use googleMaps, I am asked by means of a dialog, whether I want to enable it? If "Yes", the location service is being enabled in the background without callings another user interaction.
Can I enable location settings without having other user interactions?

No you cannot. location information is build into the system, which require permission. Android M currently has "permission" build for each app that can set it when app starts. But if user forcefully turn off GPS then the user has to turn it back on him/herself.

Related

App crashing while disabling App location

I have a service ongoing which takes current location and sends it to the server.
While App is in the background with Service on, if I go to settings and turn off my App Location, the app is crashing I'm unable to able to catch the crash.
But if I kill the app and re-run it again the crash does not happen at that time.
It only happens when the app is in the background and at the same time app location permission is turned off.
Thanks & Regards
Syed
You could register for the LocationManager.PROVIDERS_CHANGED_ACTION in the BroadcastReceiver. When it is hit, you can check if the provider is still enabled and use that accordingly to prevent your app from crashing when someone revokes the location permission.
LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
boolean enabled = locationManager != null && locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
Quoting
I mean if i have app in background and i turn my app location
permission off then if i open my app from background it crashes at
that time only. If i kill and open my app then it works fine. Im not
able to produce Exception/Error logs
The behavior you are observing is correct. You need to explicitly check for the Location Permission before registering/starting location update listener.

Mock location for specific app only

I want to set mock location for specific app only. So far, what I have understood is, if I set GPS provider with some mock location then, all apps accessing location through GPS will receive mocked location. Can it be made app specific? (means only specific app should see mocked location, all other apps will see true location.) If yes , how?

Prevent user to change GPS status Fused Location API

I'm using the Google API (Fuse Location Provider) to track a device's location but I need to prevent the user from disabling the GPS. I read a lot of answers but all I've found is: No, can't do, rooting the device or bug exploits. A friend just showed me a parental control app that accomplishes this without having to root the device. About user privacy, etc. The code will be used only in company's devices with the users being aware of this.
At least I need a way to detect if the GPS has been deactivated to notify the user, by the way I can't find any reference to this in the Google API.
Thanks a lot in advance.
GpsStatus.Listener - Try reading this.
You can override the method onGpsStatusChanged:
#Override
abstract void onGpsStatusChanged(int event){
if (event==GPS_EVENT_STOPPED){
LocationManager mlocManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
boolean enabled = mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(!enabled){
//toggle your notification
}
}
}

How to disable location service programmatically?

Is there any way to disable google location service programmatically?
I want to write an app to turn it off with a widget.
Thanks for your help!
Is there any way to disable google location service programmatically?
No, sorry, you cannot disable GPS, etc. programmatically, except via an app installed on the system partition or signed by the firmware's signing key. IOW, you need a rooted phone.
As far as i know, the LocationManager is a system service that can not be turned off. Though, you can disable all the location providers that the service uses, such as Wireless location, GPS, etc.
If you're using the UiAutomater, you can turn off Location Services by simulating a click on the Location option in the Quick Settings. The filter logic may be platform dependent. You may analyse the view hierarchy using the uiautomatorviewer.
i.e.
UiDevice.getInstance(getInstrumentation()).openQuickSettings();
UiDevice.getInstance(getInstrumentation()).findObject(new UiSelector().descriptionContains("Location").className(ViewGroup.class)).click();
build.gradle
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
Short answer is, No. But if you really need to turn it off, you can prompt the user to do it:
LocationManager loc = (LocationManager) this.getSystemService( Context.LOCATION_SERVICE );
if( loc.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER ) )
{
Toast.makeText( this, "Please turn off GPS", Toast.LENGTH_LONG).show();
Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS );
startActivity(myIntent);
}
else
{
// Do nothing
}
This might not help your situation but is the only way I know of changing the settings.

How to enable Network Location provider automatically

I am fetching Latitude and Longitude through network
It is giving me latitude and longitude but what if my NETWORK LOCATION PROVIDER is unchecked
It will never provide me the Latitude and Longitude of the current location.
What i want is, How Can I enable the NETWORK LOCATION PROVIDER in phone without knowing
user that It is opened,
How can i do that?
What i have tried is:
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, 1);
But this will open the page in my phone to check it i want to check AUTOMATICALLY
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.android.phone", "com.android.phone.Settings");
startActivity(intent);
It is impossible to enable the network provider location automatically as already mentioned. The correct way is to display a box and redirect the user to the settings page. You can enable GPS by using a slight hack as mentioned here. Still I wouldn't suggest doing that.
Not possible to enable it automatically. Network settings must be enabled manually by user only. You can show a message to the user to enable his network.
You can able to open the Location Services page to enable it. By programmatic, not possible.

Categories

Resources