1

我正在使用 Location 开发 android 应用程序。我可以使用以下代码获取当前位置。

  public void GetLocation()
     {
        boolean isGPSEnabled = false;
        boolean isNetworkEnabled = false;
        private LocationManager mLocationManager;
        private String mProvider;
        mLocationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();     
        isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if(isGPSEnabled && isNetworkEnabled)
        {
            mProvider = mLocationManager.getBestProvider(criteria, false);
            Location location = mLocationManager.getLastKnownLocation(mProvider);           
            Sring mLatitude=String.valueOf(arg.getLatitude());           
            String mLongitude=String.valueOf(arg.getLongitude());            
        }
    }

一旦位置不经常更改,我需要在后台更新用户的位置。我怎样才能做到这一点?

4

2 回答 2

0
public class CurrentLatLng implements LocationListener {

    public static final int GPS_NOT_ENABLED = -1;
    public static final int VALID = 1;

    LocationManager manager;
    Context context;

    public CurrentLatLng(Context context) {
        this.context = context;
    }

    public void getCurrentLatLng() {

        //Check if GPS is enabled
        if (Commons.isGPSEnabled(context)) {
            manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10, 10, this);
        } else {
            // GPS NOT ENABLED. EVEN THEN THE LOCATION WILL BE RECIEVED AS WE ARE GETTING LOCATION BY NETWORK_PROVIDER
        }
    }

    @Override
    public void onLocationChanged(Location l) {
        // HERE YOU WILL GET THE LATEST LOCATION AND WILL BE UPDATING WHENEVER YOU CHANGE YOUR LOCATION. 
    }
}
于 2013-10-04T05:56:28.667 回答
0

您需要使用位置侦听器。您可以在特定米或特定时间间隔内收听位置。检查本教程。

locationManager.requestLocationUpdates(

                LocationManager.GPS_PROVIDER,

                **MINIMUM_TIME_BETWEEN_UPDATES**,

                **MINIMUM_DISTANCE_CHANGE_FOR_UPDATES**,

                new MyLocationListener()

        );
于 2013-10-04T05:54:18.960 回答