String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider != null ){
if(! provider.contains("gps")){
// Notify users and show settings if they want to enable GPS
new AlertDialog.Builder(MessagePage.this)
.setMessage("GPS is switched off. enable?")
.setPositiveButton("Enable GPS", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, 5);
}
})
.setNegativeButton("Don't do it", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == 5 && resultCode == 0){
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider != null){
switch(provider.length()){
case 0:
//GPS still not enabled..
break;
default:
Toast.makeText(this, "GPS is now enabled.", Toast.LENGTH_LONG).show();
break;
}
}
}
else{
//the user did not enable his GPS
}
}