How to disable location service programmatically? - android

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.

Related

How to enable location on Android in program code?

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.

How can i enable and disable NETWORK_PROVIDER programatically

We all know about that GPS Issues on Android.
How do we create the same turnOnGps Function Bellow to a NETWORK_PROVIDER, I want turn on Both.
private void turnGPSOn(){
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.contains("gps")){
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);
} }
Just adding onto what CommonsWare mentioned, that method only works on Android versions of 2.2 and below.
However, you can use the, simple, code below:
startActivity(new Intent("android.settings.LOCATION_SOURCE_SETTINGS"));
This will take the user directly to the screen where they can enable/disable Location based options.
Refer here (docs), for more settings
Hope this helps
Fortunately, the script-kiddie technique you cite above does not work on modern versions of Android. You cannot enable or disable any location provider yourself, for blindingly obvious privacy reasons. Please allow the user to enable or disable location providers through the OS-supplied mechanisms (Settings, Power Control app widget, etc.).

Check If Google Location & Search is enabled

It seems that from Android OS version 4.1 there is a new setting, wich allows google applications use the user location.
I am developping a app that use the google maps api v2, and I made it location aware by using the following line:
mMap.setMyLocationEnabled(true);
Now i want to check if the user has checked the location & google search at the settings screen.
There is no problem reading if the wifi and the gps is enabled, but i can not find the way to check of the google location & search is enabled until now i have just found the following piece of code:
try {
int gEnable= settings.System.getInt(getActivity().getContentResolver(),Settings.ACTION_SEARCH_SETTINGS);
} catch (SettingNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
But it always throws the exception.
Thank you very much for reading my post.
EDIT
I will try to explain it more clear....
In my app I Just want to check programmaticaly if the "Location and google search" (as i show in the picture below) is checked.
Thank u very much for reading my question.
I don't completely understand your question, though considering to do something like this might be also a way.
boolean enabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// Check if GPS is enabled, if not send user to GPS settings
if (!enabled) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, 0);
}
besides LocationManager.GPS_PROVIDER you could also ask for
LocationManager.PASSIVE_PROVIDER, which is "a special location provider for receiving locations without actually initiating a location fix" or
LocationManager.NETWORK_PROVIDER, which uses the network location. Google doesn't do anything else than using the last two options in my opinion...
I hope that helps...

Android : Turning GPS on Programmatically [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Enable GPS programatically like Tasker
All,
I seen most of the answers in this forum that we CANT turn on gps programmatic ally. I am wondering in some of the applications available in Android market, how do they do? I tried most of the things explained in this forum, it always gives a prompt to the user to enable the Location based services.
Any other thoughts?
Thanks,
Ramesh
Not sure what you mean by "turn it on". Do you mean enable or disable it? This thread discusses some ways to enable or disable it by exploiting bugs: How can I enable or disable the GPS programmatically on Android?. I wouldn't use this method though, since it won't work on devices where the bug has been fixed. Far better to forward the user to the settings app and let them enable gps.
Do you mean to turn it on or off to use it, assuming it's been enabled? You don't need to do that. Simply call LocationManager.requestLocationUpdates() with the right location provider, and the system will turn it on for you.
Have you looked here?
http://developer.android.com/guide/topics/location/strategies.html
What you can do is assert what type of location provider is enabled, check if it is GPS, if not add this to prompt the user to turn on GPS (be sure to run a long Toast to let the user know to enable GPS location and to hit the back button once enabled):
ComponentName intentComponent = new ComponentName("com.android.settings", "com.android.settings.LocationSettings");
Intent mainIntent = new Intent("android.intent.action.MAIN");
mainIntent.setComponent(intentComponent);
startActivity(mainIntent);

How to turn on and turn off (enable and disable) Location Services programatically in ICS?

I'd like to know if it possible to enable and disable Location Services programatically in Android 4.0?
I've found several approaches how to do this for previous versions of Android (for instance, this is the most popular link). But these approach does not work for Android ICS.
Also, I understand that an application should not do this but, for instance, default widget does this.
Can anybody clarify if it is possible and how to achieve this?
In a tiny app I have developed, I turn off the gps by just disabling the location on the MyLocationOverlay. Of course, this will work only if you are using maps from Google as that class belongs to the Google API's.
myLocationOverlay.disableMyLocation();
I place the line on the onStop() method and only if it is currently enabled. And every time my app goes to the background, the GPS turns automatically off.
Hope it helps.
You can enable and disable gps device but users must confirm in setting widget.
The solution in this topic.
LocationManager L = (LocationManager) getSystemService(LOCATION_SERVICE);
if (!L.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Intent I = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(I);
}

Categories

Resources