gpt4 book ai didi

android - 如何在 GPS 和网络提供商之间切换?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:33:58 26 4
gpt4 key购买 nike

在我的应用中,我想同时使用这两个位置提供程序。

这意味着,如果禁用了 gps,我希望我的应用程序切换到网络,并在启用 gps 时切换回它。

我正在使用两个 LocationListener 来处理这两个请求。

public void onStatusChanged(String provider, int status,Bundle extras) 
switch (status) {
case LocationProvider.TEMPORARILY_UNAVAILABLE:
......
break;
case LocationProvider.OUT_OF_SERVICE
.....
break;
case LocationProvider.AVAILABLE
.....
break;
}

在每个监听器中,我在 onStatusChanged() 中检测到这些状态。原来,这个方法会在第一次更改(禁用网络)时使用,但是当我再次启用网络时,它什么也没有显示,这是为什么?监听器不会一直检测状态??

请帮助我,最好在代码中发布您的解决方案...谢谢!

最佳答案

这是我的做法:

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);
}
}

关于android - 如何在 GPS 和网络提供商之间切换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8047867/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com