(这是关于android中的LocationManager
类)。
有没有办法使用requestLocationUpdates
,但在某种程度上,它允许每次给我最佳活跃提供者的结果?我可以将getBestProvider
的结果传递给它,但是它会始终从该提供程序返回结果,如果用户打开/关闭gps,它将无法正常工作。
我的代码在后台服务中。
答案 0 :(得分:3)
有没有办法使用requestLocationUpdates,但在某种程度上允许它每次都为最佳活跃提供商提供结果?
不,尽管欢迎您为多个提供商注册单独的LocationListeners
。
我可以将getBestProvider的结果传递给它,但是它总会返回该提供程序的结果,如果用户打开/关闭gps,它将无法正常工作。
启用和禁用其提供商时,会通知您LocationListener
。如果您的当前提供程序已禁用,则可以向备份提供程序注册LocationListener
。
答案 1 :(得分:1)
这就是我的所作所为:
public class LocationActivity extends Activity implements LocationListener{
private TextView latituteField;
private TextView longitudeField;
private LocationManager locationManager;
private String provider;
private TextView outputField;
private Location location;
private ScrollView scrollView;
private Criteria criteria;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
latituteField = (TextView) findViewById(R.id.lat_textView);
longitudeField = (TextView) findViewById(R.id.long_textView);
outputField = (TextView) findViewById(R.id.output_textView);
scrollView = (ScrollView) findViewById(R.id.scrollView1);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
List<String> providers = locationManager.getProviders(criteria, true);
outputField.append("Providers available..." + "\n");
for (String provider : providers) {
outputField.append(provider + "\n");
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
provider = locationManager.getBestProvider(criteria, true);
locationManager.requestLocationUpdates(provider, 400, 1, this);
if (provider != null) {
outputField.append("Provider " + provider + " has been selected." + "\n");
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
if (location != null) {
onLocationChanged(location);
} else {
latituteField.setText("Location not available");
longitudeField.setText("Location not available");
}
} else {
outputField.append("No provider selected" + "\n");
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
}
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) {
double lat =location.getLatitude();
double lng =location.getLongitude();
latituteField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));
outputField.append("New Location: " + String.valueOf(lat) + " " + String.valueOf(lng) + "\n");
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
@Override
public void onProviderDisabled(String dProvider) {
outputField.append("Provider " + dProvider + " has been disabled." + "\n");
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
provider = locationManager.getBestProvider(criteria, true);
locationManager.requestLocationUpdates(provider, 400, 1, this);
if (provider != null) {
outputField.append("Provider " + provider + " has been selected." + "\n");
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
if (location != null) {
onLocationChanged(location);
} else {
latituteField.setText("Location not available");
longitudeField.setText("Location not available");
}
} else {
outputField.append("No provider selected" + "\n");
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
}
@Override
public void onProviderEnabled(String eProvider) {
outputField.append("Provider " + eProvider + " has been enabled." + "\n");
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
provider = locationManager.getBestProvider(criteria, true);
locationManager.requestLocationUpdates(provider, 400, 1, this);
if (provider != null) {
outputField.append("Provider " + provider + " has been selected." + "\n");
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
if (location != null) {
onLocationChanged(location);
} else {
latituteField.setText("Location not available");
longitudeField.setText("Location not available");
}
} else {
outputField.append("No provider selected" + "\n");
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
outputField.append("Provider " + provider + " status changed to: " + Integer.toString(status) + "\n");
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
}
答案 2 :(得分:0)
从API级别9开始,可以使用此方法:
requestLocationUpdates(long minTime, float minDistance, Criteria criteria, PendingIntent intent);