Turn On Off GPS in ICS [duplicate] - android

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
ICS Android enable gps programmatically? [closed using an alternative approach]
I was using the code below to enable and disable GPS programmatically and worked perfectly in 2.2 and 2.3.
Intent i = new Intent();
i.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
i.addCategory(Intent.CATEGORY_ALTERNATIVE);
i.setData(Uri.parse("3"));
context.sendBroadcast(i);
However, when trying to use the ICS (4.0) realized it did not work. Then I discovered that it was a bug as link below.
http://code.google.com/p/android/issues/detail?id=7890
I believe we have fixed in ICS.
Could anyone tell me if there is an alternative to solve this in ICS?
I think an OS ridiculous "open source" have so many restrictions. I'm already starting to regret having opted to develop our applications on Android.

I believe it is a security feature that you cannot programmatically alter the GPS state.
Why does open source mean it shouldn't have restrictions? I'm confused. Open source just means you have access to the source code.
On a personal note, developing for Android was much less painful than developing for iOS using Cocoa Touch.
EDIT:
Try using this to let the user know they need to enable GPS.
(source: http://manishkpr.webheavens.com/android-syste-gps-setting-intent/)
private void checkGPS(){
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L,1.0f, this);
boolean isGPS = locationManager.isProviderEnabled (LocationManager.GPS_PROVIDER);
if(isGPS){
showToast("Gps On");
}else{
startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
}
}

Those restrictions are because of security issues. You should not try to achieve this case because it works now, but in next few days can be fixed - for example custom roms.
The best solution to manage GPS is to open Android's GPS preferences and let user to enable/disable GPS manually.

Related

LocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) is not reliable, why?

One user of my app reported that app tells network for location is off even he did turn it on. He sent me few screen shots and they made me think;
LocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
is not working properly. His phone is running Android 4.1.2 and first I thought this is the cause of this issue. But it was not the case. He sent me a screen shot of that setting too.
Then I googled and found this. The question seems to help me but unfortunately answer was not helpful for this case and questioner did not pursue farther.
My app is related to location and have been using LocationManager.isProviderEnabled to know GPS and Network for location is on or off. I have never been told my app is not properly knowing those settings until recently. He is the first user who reported the issue. I learned there are another method to know GPS and Network for location settings, by seeing Secure.LOCATION_PROVIDERS_ALLOWED. To see how this method work on his phone, I wrote simple app and asked him to run. This app does simple task and shows text on screen.
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
string = "GPS=on\n";
}
else
{
string = "GPS=off\n";
}
if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
{
string += "Network=on\n";
}
else
{
string += "Network=off\n";
}
String status = android.provider.Settings.Secure.getString(getContentResolver(), Secure.LOCATION_PROVIDERS_ALLOWED);
if(status.contains("gps"))
{
string += "GPS=on\n";
}
else
{
string += "GPS=off\n";
}
if(status.contains("network"))
{
string += "Network=on\n";
}
else
{
string += "Network=off\n";
}
He sent back screen shot again. It looks;
GPS=on
Network=off
GPS=on
Network=on
This result did not make me happy. There could be some possibilities for this.
As other person questioned before, this issue has been there on some phones.
Google broke this with 4.1.2. isProviderEnabled does not work on this version.
Although not documented, starting 4.1.2, isProviderEnabled won't work as it did before.
No, Google changed anything. This is a bug for this particular phone.
Now my questions are;
Is LocationManager.isProviderEnabled still valid for Android 4.1.2 and later?
Does seeing Secure.LOCATION_PROVIDERS_ALLOWED have some drawbacks/pit holes (when I gave up using LocationManager.isProviderEnabled?
Thanks in advance.
EDIT1:
Here you can download test app from Google Play to try or ask someone to try.
EDIT6:
I removed test app since this question is answered.
EDIT2:
I released my app which checks network provider is usable by seeing Secure.LOCATION_PROVIDERS_ALLOWED and got exception on limited phones.
These are ACRA's report.
Some phone running OS 4.1.1.
java.lang.IllegalArgumentException: requested provider network doesn't exisit
at android.os.Parcel.readException(Parcel.java:1434)
at android.os.Parcel.readException(Parcel.java:1384)
at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:675)
at android.location.LocationManager._requestLocationUpdates(LocationManager.java:686)
at android.location.LocationManager.requestLocationUpdates(LocationManager.java:508)
Some phone running OS 4.1.2.
java.lang.IllegalArgumentException: provider=network
at android.os.Parcel.readException(Parcel.java:1439)
at android.os.Parcel.readException(Parcel.java:1389)
at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:659)
at android.location.LocationManager._requestLocationUpdates(LocationManager.java:690)
at android.location.LocationManager.requestLocationUpdates(LocationManager.java:512)
I have never seen those exceptions until I changed a method to check network provider for location is usable or not. So I think LocationManager.isProviderEnabled is safe and seeing Secure.LOCATION_PROVIDERS_ALLOWED is risky. But this will put me back to original issue. Why LocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) returns false (and there is not really) when Secure.LOCATION_PROVIDERS_ALLOWED tells there IS. Is Android OS poorly designed? Or I have just seeing issues tied only to specific (but there are at least 2 of them) phones?
EDIT3:
I updated test app to show GPS/Network location provider seems really usable or not by accessing with requestLocationUpdates().
And I disclose 2 phones name.
1) SBM200SH, OS4.1.2, Softbank mobile, Sharp Corporation
2) HTX21 (INFOBAR A02), OS4.1.1, KDDI, HTC
EDIT4:
I found 3rd phone.
3) SBM203SH, OS4.1.2, Softbank mobile, Sharp Corporation
EDIT5:
Sharp Corporation is running discussion space for mobile developers. I posted topic by presenting this SO's question. I hope someone at Sharp Corporation takes action for this. I will keep this updated.
Developer support provided by Sharp corporation is excellent and they answered to my question in less than 48 hours.
This is what I got from them.
There are 2 conditions must be met that LocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) returns true.
Some internal state is ready for network location.
Network location is enabled on setting screen.
Second one is obvious. But first one is not. They told how to simulate first one is negative. You can confirm the issue with steps shown below and running my test app (please see my question for link to download).
Open settings of you phone.
Tap Applications.
Tap All tab.
Find "Network Location", tap it.
Tap "Disable".
Reboot your phone.
Run test app.
For reason I can't understand the user's phone failed to do something related to first condition shown above and exhibits the issue.
Conclusion:
LocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) is reliable. And be aware, Secure.LOCATION_PROVIDERS_ALLOWED is less reliable.
The modern way to check the users Location settings is through LOCATION_MODE in Settings.Secure
For example if you simply want to know if the user has disabled them or not, you can do:
public static boolean isLocationEnabled(Context context) {
return getLocationMode(context) != Settings.Secure.LOCATION_MODE_OFF;
}
private static int getLocationMode(Context context) {
return Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);
}
This will return true if Location is enabled. If you need finer granularity see the docs for details.
This method is better suited when using the Google Services Location APIs than the old NETWORK_PROVIDER and GPS_PROVIDER ways. Note: Requires KitKat / API19
Not directly an answer to your question(s), but check out the new Location API that Google launched last week. It's really easy to implement and it will check for the best possible location without wasting battery.
http://developer.android.com/google/play-services/location.html
and here's a session at Google I/O about this new API and how to use it.
http://www.youtube.com/watch?v=Bte_GHuxUGc
This way you don't need to worry about checking if the GPS is on or not and stuff like that
Location Manager is not reliable on some phones. You may notice that if you launch google maps all of a sudden your app works. That is because Google Maps kicked the LocationManager. Which also means that there is programmatic way to kick that dude alive. So I used
HomeScreen.getLocationManager().requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onLocationChanged(final Location location) {
}
});
After the above code, I called what ever I needed from LocationManager and it kinda worked. If not try out the new API's LocationClient. This is suppose to be much better, battery, accuracy and reliability.

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