Using requestLocationUpdates for more than one provider

我与影子孤独终老i 提交于 2019-12-06 10:49:11

问题


(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.


回答1:


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.




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/4150462/using-requestlocationupdates-for-more-than-one-provider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!