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

时间:2011-11-08 08:47:55

标签: android networking gps location

在我的应用中,我想使用两个位置提供商。

这意味着,如果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()中检测到这些状态。 事实证明,这种方法将用于第一次更改(禁用网络),但是当我再次启用网络时,它什么都没有显示。为什么?监听器不会一直检测到状态??

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

2 个答案:

答案 0 :(得分:4)

这就是我的所作所为:

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

答案 1 :(得分:2)

你的答案在What is the simplest and most robust way to get the user's current location in Android?。在这个Fedor中,很好地解释了如何在位置提供者之间切换。

感谢。