这是我的代码。实际上,我想使用这项服务来获得用户的新坐标。在我的测试中,它没有奏效。我没有找到一个好位置。似乎从来没有打电话给移动GPS来更新坐标。我的代码有错误或手机有问题吗?感谢
public class LocationService extends Service implements LocationListener {
Boolean isWifiEnable;
Boolean isGPSEnable;
Context context;
Location location;
private static final long MIN_DISTANCE = 10;
private static final long MIN_TIME = 1000 * 60;
public LocationService(Context context) {
this.context = context;
bestLastKnownLocation();
}
public Location getLocation() {
return location;
}
public Location bestLastKnownLocation() {
location = null;
String provider = null;
// Acquire a reference to the system Location Manager
LocationManager lm = (LocationManager) context.getSystemService(LOCATION_SERVICE);
// location = lm.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
isGPSEnable = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
isWifiEnable = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
// Define a listener that responds to location updates
if (isGPSEnable == false && isWifiEnable == false) {
// no network provider is enabled
//showToastInAsync("No provider");
} else {
if (isWifiEnable) {
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME, MIN_DISTANCE, this);
provider = LocationManager.NETWORK_PROVIDER;
Log.d("Network", "Network");
}
if (isGPSEnable) {
if (lm == null) {
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DISTANCE, this);
provider = LocationManager.GPS_PROVIDER;
Log.d("GPS", "GPS");
}
}
if (lm != null) {
location = lm.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
//在这里其他PROVIDER不起作用。只有被动的工作才是奇怪的。
}
}
return location;
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}