The application I’m currently working requires that the user has the GPS active on his/her phone. This can be either the coarse (cell tower triangulation) or fine (GPS satellites) methods, but whichever it is, the application needs the Geodata to function. This brought up the issue of, how do we know if the user actually has the GPS enabled on the phone? If they don’t, the application will still work, but all the coordinates it stores will end up being 0 degrees for both latitude and longitude, not very useful. To combat this, we can do a simple check when the application starts up to make sure the user knows that their GPS is disabled, and their location will be recorded as somewhere along the equator.
To start with, we need a locationManager object:
LocationManager locManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Now that we have a location manager, its a simple process to check to see if the GPS is enabled or not:
if (!locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
createGpsDisabledAlert();
}
This just checks to see if GPS is on, and if not, it calls a method, createGpsDisabledAlert(), which will build an alert dialog to warn the user that their GPS is off:
private void createGpsDisabledAlert(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your GPS is disabled! Would you like to enable it?")
.setCancelable(false)
.setPositiveButton("Enable GPS",
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
showGpsOptions();
}
});
builder.setNegativeButton("Do nothing",
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
If the user decides they want to turn their GPS on, and they select the positive action of the dialog, showGpsOptions() is called. All this method does is to show the “Locations and Security” page from the Android settings menu. As a small sidenote, as of Android 1.5, there is no way to directly toggle the location settings through code, so we have to assume the user can figure it out if shown the proper screen. The code for showGpsOptions() is very simple:
private void showGpsOptions(){
Intent gpsOptionsIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(gpsOptionsIntent);
}
And thats it! When the user starts the activity, if the GPS is off, the dialog will pop up asking them what they want to do. If the GPS is already on, nothing will happen. Sweet. The full code looks like the following:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (!locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
createGpsDisabledAlert();
}
}
private void createGpsDisabledAlert(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your GPS is disabled! Would you like to enable it?")
.setCancelable(false)
.setPositiveButton("Enable GPS",
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
showGpsOptions();
}
});
builder.setNegativeButton("Do nothing",
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
private void showGpsOptions(){
Intent gpsOptionsIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(gpsOptionsIntent);
}