// Retrieve a list of location providers that have fine accuracy, no monetary cost, etcCriteriacriteria=newCriteria();criteria.setAccuracy(Criteria.ACCURACY_FINE);criteria.setCostAllowed(false);...StringproviderName=locManager.getBestProvider(criteria,true);// If no suitable provider is found, null is returned.if(providerName!=null){...}
@OverrideprotectedvoidonStart(){super.onStart();// This verification should be done during onStart() because the system calls// this method when the user returns to the activity, which ensures the desired// location provider is enabled each time the activity resumes from the stopped state.LocationManagerlocationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);finalbooleangpsEnabled=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);if(!gpsEnabled){// Build an alert dialog here that requests that the user enable// the location services, then when the user clicks the "OK" button,// call enableLocationSettings()}}privatevoidenableLocationSettings(){IntentsettingsIntent=newIntent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);startActivity(settingsIntent);}