Reputation:
I want to find out the GPS status, when I launch the application. For that I previously used LocationListener
class,
It was showing the status of the GPS when changing the GPS status in device only.
But here is my requirement: when I launch the app first it should check the GPS status, if GPS is available, only then I want to start the my activity and otherwise it needs to display some message to user like "Please enable GPS."
How can I do this?
Upvotes: 1
Views: 905
Reputation: 32233
you can check your provider is null and ask the user to go to system settings to enable GPS
here is the code
locationManager = (LocationManager) getSystemService(context);
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setSpeedRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_HIGH);
provider = locationManager.getBestProvider(criteria, true);
if(provider==null){
new AlertDialog.Builder(this)
.setTitle("Location Settings")
.setMessage("Application needs your location,do you want to go to\n" +
"system settings to enable your location source?")
.setIcon(android.R.drawable.ic_menu_mapmode)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
try{
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}catch(Exception e){
e.printStackTrace();
}
}})
.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
LocationBasedServicesV1.this.finish();
}
})
.show();
}
Upvotes: 1
Reputation: 318
I thinks this method can help you:
public static boolean isGPSAvailable(Context ctx){
LocationManager lm = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
return lm != null &&
(lm.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER));
}//isGPSAvailable
Upvotes: 1