11

I know this question been asked a lot here, but just wondering since ICS got released, is there a way to enable gps programmatically?

am trying to enable gps and send the location of the device to an email, the app will start when it receives an sms message.

casperOne
  • 73,706
  • 19
  • 184
  • 253
HeWhoProtects
  • 477
  • 1
  • 10
  • 21

4 Answers4

11

is there a way to enable gps programmatically?

I hope not, as that would be a privacy flaw. AFAIK, all known holes that allowed malware to enable GPS programmatically have now been closed.

am trying to enable gps and send the location of the device to an email

The user controls whether GPS is enabled or not.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • if that's the case, is there a way to run a function in the background as soon as the gps has been enabled? – HeWhoProtects Apr 24 '12 at 17:40
  • 1
    Well, there is the [`GpsStatus.Listener`](http://developer.android.com/reference/android/location/GpsStatus.Listener.html) and there are three state methods in the [`LocationListener`](http://developer.android.com/reference/android/location/LocationListener.html). – Octavian Helm Apr 24 '12 at 17:46
  • hmm that might be a way around it ... I will have a look, thanks Oct – HeWhoProtects Apr 24 '12 at 18:00
4

As Mark said, I sure hope not because turning things on that the user want's off is certainly not a very nice thing to do ;-)

You can ask the user to do so of course (which you probably already found, but for reference here is a trivialized way to do so from memory)

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);  
}
Idistic
  • 6,281
  • 2
  • 28
  • 38
  • 1
    haha yeah ... but I don't think a thief will follow my instructions to enable gps – HeWhoProtects Apr 24 '12 at 17:50
  • @HeWhoProtects Ok so Use Case: Phone Stolen, Users sends SMS, Thief was smart and turns off GPS but not SMS. The thief could always wipe phone of app's in which case not much you can do. I think you are on the right track to bypass a typical thief that might be a bit savvy but not too savvy, simply let a service accept the sms, if gps is off using detection method then que for gps ping when it's back on. – Idistic Apr 24 '12 at 17:58
  • that's what am going with for now, am going to learn how to do push notifications and replace sms, it doesn't need to be in a theft situation, losing a phone or something – HeWhoProtects Apr 24 '12 at 18:24
3

To ask user to enable GPS, check out this link

Below would be the innards of a function that attempts to get the last known GPS location in descending accuracy order. If the user does not have GPS enabled, you cannot enable it. The best you can do is grab the last known location, or prompt them to enable GPS. Everytime the user uses GPS in another app and obtains a new coordinate, or connects to a wireless network the last known location will change. I suppose you could write a background service that polls this data every 5-10 minutes and sends it to you. Although, I don't agree with the direction you seem to be heading with this application. lol. For Science!

LocationManager lm = (LocationManager)act.getSystemService(Context.LOCATION_SERVICE);
Criteria crit = new Criteria(); 
crit.setAccuracy(Criteria.ACCURACY_FINE); String provider = lm.getBestProvider(crit, true); 
Location loc = lm.getLastKnownLocation(provider);

if(loc){
    return loc;
}
lm = (LocationManager)act.getSystemService(Context.LOCATION_SERVICE);
crit = new Criteria(); 
crit.setAccuracy(Criteria.ACCURACY_COARSE); 
provider = lm.getBestProvider(crit, true); 
loc = lm.getLastKnownLocation(provider);

if(loc){
    return loc;
}
lm = (LocationManager)act.getSystemService(Context.LOCATION_SERVICE);
crit = new Criteria(); 
crit.setAccuracy(Criteria.NO_REQUIREMENT); 
provider = lm.getBestProvider(crit, true); 
loc = lm.getLastKnownLocation(provider);
return null;
JustinDanielson
  • 3,155
  • 1
  • 19
  • 26
  • which part of my "direction" you didn't agree with lol? I am still a noob at Android development so please if there is a better way of doing things I am more than open about criticisms :D – HeWhoProtects Apr 24 '12 at 18:30
  • Well from the question, it seemed like you could be implementing a stalker app. Are you just trying to use GPS or are you trying to gain GPS information discretely without the user knowing? (ex: not using permissions for gps) – JustinDanielson Apr 25 '12 at 01:21
  • no man my app is not for evil intentions, am creating a tracking service, where if the user loses their phone, they can login into my website and send a push notification to their own phone to send a gps signal, play an alarm sound(if the phone was on silent) etc... – HeWhoProtects Apr 25 '12 at 10:59
  • http://www.helloandroid.com/tutorials/calling-system-settings-android-app-gps-example might be what you're looking for then. – JustinDanielson Apr 25 '12 at 13:24
0

You have to ask the user for Permission:

Intent gpsOptionsIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(gpsOptionsIntent);
Thiago
  • 12,778
  • 14
  • 93
  • 110