import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.GpsStatus.Listener;
import android.location.LocationManager;
public class GpsCheck{
public Context context;
LocationManager manager;
public GpsCheck(Context context){
this.context=context;
this.manager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
Listener gpsStatusListener = new Listener(){
public void onGpsStatusChanged(int arg0) {
gpsEnable();
}
};
this.manager.addGpsStatusListener(gpsStatusListener);
}
public boolean gpsEnable(){
//checks weather the GPS is enabled. If not, prompts the user to enable it then checks again. Returns false if the user chooses not to enable the application.
if(!checkGpsEnabled()){
promptEnableGps();
}
return true;
}
private boolean checkGpsEnabled(){
this.manager = (LocationManager)context. getSystemService(Context.LOCATION_SERVICE);
if ( !this.manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
return false;
}
return true;
}
private boolean promptNoGpsQuit(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setMessage("Without enabling GPS this app can not function, Would you like to quit?");
alertDialogBuilder.setCancelable(false);
DialogInterface.OnClickListener onClick = new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
Intent callGPSSettingIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(callGPSSettingIntent);
}
};
alertDialogBuilder.setPositiveButton("Goto Settings Page To Enable GPS", onClick);
DialogInterface.OnClickListener onClickCancel = new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
}
};
alertDialogBuilder.setNegativeButton("Quit", onClickCancel);
AlertDialog alert = alertDialogBuilder.create();
alert.show();
return true;
}
private boolean promptEnableGps(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setMessage("GPS is disabled in your device. Would you like to enable it?");
alertDialogBuilder.setCancelable(false);
DialogInterface.OnClickListener onClick = new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
Intent callGPSSettingIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(callGPSSettingIntent);
}
};
alertDialogBuilder.setPositiveButton("Goto Settings Page To Enable GPS", onClick);
DialogInterface.OnClickListener onClickCancel = new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
promptNoGpsQuit();
}
};
alertDialogBuilder.setNegativeButton("Cancel", onClickCancel);
AlertDialog alert = alertDialogBuilder.create();
alert.show();
return true;
}
}