LocationManager.requestLocationUpdates add or update?

最后都变了- 提交于 2019-12-22 08:52:52

问题


I am looking at LocationManager to get best location for periodical reporting location. I am curious about LocationManager.requestLocationUpdates method. What would it happen if I invoke this method several times with different minTime and minDistance without calling removeUpdates? Will it add new request or just update the existing one?
I am trying to test this, it is not bit easy at the moment. Your answer would be appreciated.


回答1:


It will update the existing request.




回答2:


You could register it multiple times. see my code below.

package com.test.locationmanager;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.widget.TextView;

public class LocationManagerStatus extends Activity {

private LocationManager locationManager;
private TextView textView;
private final LocationListener gpsLocationListener =new LocationListener(){

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        final String tvTxt = textView.getText().toString();
        switch (status) {
        case LocationProvider.AVAILABLE:
            textView.setText(tvTxt + "GPS available again\n");
            break;
        case LocationProvider.OUT_OF_SERVICE:
            textView.setText(tvTxt + "GPS out of service\n");
            break;
        case LocationProvider.TEMPORARILY_UNAVAILABLE:
            textView.setText(tvTxt + "GPS temporarily unavailable\n");
            break;
        }
    }

    @Override
    public void onProviderEnabled(String provider) {
        textView.setText(textView.getText().toString()
                + "GPS Provider Enabled\n");
    }

    @Override
    public void onProviderDisabled(String provider) {
        textView.setText(textView.getText().toString()
                + "GPS Provider Disabled\n");
    }

    @Override
    public void onLocationChanged(Location location) {
        locationManager.removeUpdates(networkLocationListener);
        textView.setText(textView.getText().toString()
                + "New GPS location: "
                + String.format("%9.6f", location.getLatitude()) + ", "
                + String.format("%9.6f", location.getLongitude()) + "\n");
    }
};
private final LocationListener networkLocationListener =
                                                    new LocationListener(){

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras){
        final String tvTxt = textView.getText().toString();
        switch (status) {
        case LocationProvider.AVAILABLE:
            textView.setText(tvTxt + "Network location available again\n");
            break;
        case LocationProvider.OUT_OF_SERVICE:
            textView.setText(tvTxt + "Network location out of service\n");
            break;
        case LocationProvider.TEMPORARILY_UNAVAILABLE:
            textView.setText(tvTxt
                    + "Network location temporarily unavailable\n");
            break;
        }
    }

    @Override
    public void onProviderEnabled(String provider) {
        textView.setText(textView.getText().toString()
                + "Network Provider Enabled\n");
    }

    @Override
    public void onProviderDisabled(String provider) {
        textView.setText(textView.getText().toString()
                + "Network Provider Disabled\n");
    }

    @Override
    public void onLocationChanged(Location location) {
        textView.setText(textView.getText().toString()
                + "New network location: "
                + String.format("%9.6f", location.getLatitude()) + ", "
                + String.format("%9.6f", location.getLongitude()) + "\n");
    }
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    textView = (TextView) findViewById(R.id.textview);
    locationManager = (LocationManager) 
            getSystemService(Context.LOCATION_SERVICE);
}

@Override
protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(
            LocationManager.NETWORK_PROVIDER, 5000, 0,
            networkLocationListener);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            3000, 0, gpsLocationListener);
}

@Override
protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(networkLocationListener);
    locationManager.removeUpdates(gpsLocationListener);
}
}



回答3:


Use new Location API's for android, they have made it much accurate and consume less battery efficiency.



来源:https://stackoverflow.com/questions/16932388/locationmanager-requestlocationupdates-add-or-update

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