Switch off GPS programmatically - android

What i have: Currently my app is giving location through gps.
what i want: Gps to turn off automatically after i exit from the application. Because it keeps on telling me the location time and again that looks odd and also gps consume a lot battery.

Looking at above comment thread it seems its possible to turn OFF GPS programatically (But seeing only 12 Upvotes)
But if you switch OFF GPS from your application programatically, what if other applications use GPS Service ?
The solution would be like
Open Settings of android and then tell user to turn OFF GPS when he exits from the application...
For this you can do like :
Intent i = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(i);
OR
You can try like
locationManager.removeUpdates(myLocationListener);
locationManager = null;
This shutdown gps for this app, but its still available for use by other apps.

This code can alter your gps.But it is not documented
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);

Even though the link Rasel posted contains some code which might work you should keep in mind that the code is exploiting a security flaw which has been fixed already (http://code.google.com/p/android/issues/detail?id=7890) and therefore shouldn't work in the near future anymore.

If you want to enable the GPS programmaticaly then copy and paste this code in your project, its working fine.
final Intent i = new Intent();
i.setClassName("com.android.settings","com.android.settings.widget.SettingsAppWidgetProvider");
i.addCategory(Intent.CATEGORY_ALTERNATIVE);
i.setData(Uri.parse("3"));
sendBroadcast(i); //if you are in broad cast receiver activity then use context.sendBroadcast(i)

Related

How turn on GPS programmatically in my android application?

If we use this code we see a message: searching for GPS. However, the GPS symbol is merely shown; GPS doesn't actually work:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
this.sendBroadcast(intent);
}
Why isn't it working ? And how to make it work correctly ?
That's not allowed anymore. If you take a look at this bug report, this hack was subverted in Android 4.4. It still works on older OS versions, though you shouldn't be using it anywhere now. To quote that report:
This doesn't work ... however, it can cause stuff to react as if the GPS status changed -- for example, the HTC One S will show the GPS icon in the status bar, even though the GPS is still disabled.
That explains why you can see the GPS icon even though it isn't actually ON. Now as for why you can't do that ...
Android's GPS technology periodically sends location data to Google even when no third-party apps are actually using the GPS function. In many Western countries this is seen as a major violation of privacy. 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.

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.).

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 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);
}

How to enable GPS in android coding [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Enable GPS programatically like Tasker
Ok, I know there is same question asked before but all answer is "NO". It is proven that enable GPS can be done in coding. Don't believe? try out an application called "LOOKOUT". I believe there is some workaround in order to achieve this.
I had read through lookout AndroidManifest.xml, what i found out they only use
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
Anyone have idea how they managed to do this? what is the secret behind? I personally managed to do this only if the application is install as system app and also require two permission
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
My Code:
Settings.Secure.setLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER, true);
thank you.
According to this post on the Lookout support forums, they are unable to programmatically enable GPS.
If Lookout could enable the GPS receiver remotely, then use the service, then turn it off – we would, but unfortunately there is no “disable GPS for everything other than Lookout” and if you have disabled the GPS receiver, no application can make use of it. Enabling the GPS receiver requires a user to do it manually.
Edit: It looks like in the new version of Lookout they are most likely now exploiting a security flaw to accomplish this. It is not guaranteed to continue to work, but according to an open Android bug report from April 2010, code similar to the following will successfully programmatically enable/disable GPS (at least until the flaw is fixed):
private void toggleGPS(boolean enable) {
String provider = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider.contains("gps") == enable) {
return; // the GPS is already in the requested state
}
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"));
context.sendBroadcast(poke);
}
Also, see the question "Enable GPS programmatically like Tasker" and the XDA thread mentioned there for more discussion on how people have used this method.
If you do use this method, you should make it clear to potential users of your application, since this is a HUGE potential privacy concern.

Categories

Resources