Using requestLocationUpdates for more than one pro

2020-07-24 08:02发布

(this is about the LocationManager class in android).

Is there a way of using requestLocationUpdates, but in some way that allows it to give me results for the best active provider every time? I could pass it the result of getBestProvider, but then it will always return results from that provider, and it wouldn't work as I expect if the user turns the gps on/off.

My code is in a background service.

3条回答
▲ chillily
2楼-- · 2020-07-24 08:12

This is what I do:

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);
    }
}
查看更多
The star\"
3楼-- · 2020-07-24 08:20

From API level 9 and onwards, this is possible, using this method:

requestLocationUpdates(long minTime, float minDistance, Criteria criteria, PendingIntent intent);

See the documentation for more information on usage.

查看更多
Fickle 薄情
4楼-- · 2020-07-24 08:28

Is there a way of using requestLocationUpdates, but in some way that allows it to give me results for the best active provider every time?

No, though you are welcome to register separate LocationListeners for multiple providers.

I could pass it the result of getBestProvider, but then it will always return results from that provider, and it wouldn't work as I expect if the user turns the gps on/off.

Your LocationListener is notified when its provider is enabled and disabled. If your current provider is disabled, you might register a LocationListener with a backup provider.

查看更多
登录 后发表回答