Android : Turning GPS on Programmatically [duplicate] - android

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

Related

how to enable GPS Automatically in android Oreo?

sorry for that because this question asked very time but i want to get user location in emergency condition so probably the user GPS possible not enable at this time also Internet.
so how i enable GPS at this time or other way to get location at this time without GPS and Internet ?
please provide code if possible? highly appreciated
android 8.1
I put my comment in answer field because of this restriction: 'You must have 50 reputation to comment'.
There is this 'Last Known Location' in the 'Fused Location Provider API'. Check them out.

How to enable GPS using code like Jugnoo app do?

Here is the app https://play.google.com/store/apps/details?id=product.clicklabs.jugnoo&hl=en which is able to turn on GPS automatically. How can I do the same?
"You can't do this for security/privacy reasons, you have to forward to location preferences screen and let the user enable/disable it."
But maybe the link below can help you out.
Check this out : How can I enable or disable the GPS programmatically on Android?
Hope this will help you.
Here is Official Google's Settings API https://developers.google.com/android/reference/com/google/android/gms/location/SettingsApi
through which you can enable GPS on single tab like jugnoo app will do.

Turn On Off GPS in ICS [duplicate]

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.

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