我目前有一个位置服务,可以在应用启动时启动。我的onCreate方法如下: -
public void onCreate() {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setCostAllowed(false);
// Get a location manager from the system services
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// if GPS is not enabled. Show alert to user.
if ( !locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
showEnableLocationsDialog();
}
// Get the location provider that best matches the criteria
String bestProvider = locationManager.getBestProvider(criteria, false);
if (bestProvider != null) {
// Get the user's last known location
location = locationManager.getLastKnownLocation(bestProvider);
MyApp myApp = (MyApp) getApplicationContext();
myApp.setLocation(location);
if (locationManager.isProviderEnabled(bestProvider)) {
locationManager.requestLocationUpdates(bestProvider, 0, 0, this);
}
}
}
当未启用GPS时,我会显示一个对话框,提示用户启用GPS。我的问题是,如果用户确实选择转到“设置”页面以启用GPS,那么当用户再次启动应用程序时,剩余的getBestProvider()和requestLocationUpdates()流将受到影响,因为该服务已在“运行”中背景? (只是猜测,因为我是整个Android开发的新手)。
我班级的完整代码如下: -
public class LocationService extends Service implements LocationListener {
private LocationManager locationManager;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setCostAllowed(false);
// Get a location manager from the system services
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// if GPS is not enabled. Show alert to user.
if ( !locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
showEnableLocationsDialog();
}
// Get the location provider that best matches the criteria
String bestProvider = locationManager.getBestProvider(criteria, false);
if (bestProvider != null) {
// Get the user's last known location
location = locationManager.getLastKnownLocation(bestProvider);
MyApp myApp = (MyApp) getApplicationContext();
myApp.setLocation(location);
if (locationManager.isProviderEnabled(bestProvider)) {
locationManager.requestLocationUpdates(bestProvider, 0, 0, this);
}
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
public void onLocationChanged(Location newLocation) {
// if a newer location is available, check if its better than current available one
MyApp myApp = (MyApp) getApplicationContext();
Location location = myApp.getLocation();
boolean isBetterLocation = isBetterLocation(location, newLocation);
if(isBetterLocation) {
location = newLocation;
}
}
public static Location getBestLocation(LocationManager locationManager) {
Location location_gps = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location location_network = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
// If both are available, get the most recent
if(location_gps!=null && location_network !=null) {
return (location_gps.getTime() > location_network.getTime())?location_gps:location_network;
}
else if(location_gps==null && location_network ==null){
return null;
}
else
return (location_gps==null)?location_network:location_gps;
}
public void onProviderEnabled(String s){}
public void onProviderDisabled(String s){}
public void onStatusChanged(String s, int i, Bundle b){}
@Override
public void onDestroy() {
locationManager.removeUpdates(this);
}
private void showEnableLocationsDialog() {
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
myAlertDialog.setTitle("No location services enabled");
myAlertDialog.setMessage("To find your location, please enable at least one service in the Settings");
myAlertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// show settings screen
Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(settingsIntent);
}}
);
myAlertDialog.setNegativeButton("Skip", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// do something when the Cancel button is clicked
}}
);
myAlertDialog.show();
}
private static final int TWO_MINUTES = 1000 * 60 * 2;
/** Determines whether one Location reading is better than the current Location fix
* @param location The new Location that you want to evaluate
* @param currentBestLocation The current Location fix, to which you want to compare the new one
*/
protected boolean isBetterLocation(Location location, Location currentBestLocation) {
if (currentBestLocation == null) {
// A new location is always better than no location
return true;
}
// Check whether the new location fix is newer or older
long timeDelta = location.getTime() - currentBestLocation.getTime();
boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
boolean isNewer = timeDelta > 0;
// If it's been more than two minutes since the current location, use the new location
// because the user has likely moved
if (isSignificantlyNewer) {
return true;
// If the new location is more than two minutes older, it must be worse
} else if (isSignificantlyOlder) {
return false;
}
// Check whether the new location fix is more or less accurate
int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
boolean isLessAccurate = accuracyDelta > 0;
boolean isMoreAccurate = accuracyDelta < 0;
boolean isSignificantlyLessAccurate = accuracyDelta > 200;
// Check if the old and new location are from the same provider
boolean isFromSameProvider = isSameProvider(location.getProvider(),
currentBestLocation.getProvider());
// Determine location quality using a combination of timeliness and accuracy
if (isMoreAccurate) {
return true;
} else if (isNewer && !isLessAccurate) {
return true;
} else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
return true;
}
return false;
}
/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
if (provider1 == null) {
return provider2 == null;
}
return provider1.equals(provider2);
}
}
另外,我读到了如何一直询问位置更新并不应该是一个好主意,而应该使用AlarmManager。对此也有任何建议吗?