ICS Android enable gps programmatically?

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.


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.


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

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;