在android上获取第一个位置并停止更新

在android上获取第一个位置并停止更新,android,Android,在android上获取第一个位置仍然有问题。我使用的标准如下 LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_COARSE); criteria.setSpeedRequire

在android上获取第一个位置仍然有问题。我使用的标准如下

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
        criteria.setSpeedRequired(true);
        String provider = lm.getBestProvider(criteria, true);

        if(provider!=null && provider.length()>0){
            lm.requestLocationUpdates(provider, 0, 0, this);
        }
当我第一次得到像lm.removeUpdates这样的位置时,我需要停止;
那行代码放在哪里?在线位置更改?

是<代码>onLocationChanged将在每次位置更新时调用。如果你只想要一个,你可以把它移到那里

public class GPSLocatorActivity extends Activity {
    double current_lat, current_lng;
    MapView mapView;
    boolean flag = true;

    /** Called when the activity is first created. */

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /* Use the LocationManager class to obtain GPS locations */

        LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        LocationListener mlocListener = new MyLocationListener();

        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1,
                mlocListener);

    }

    /* Class My Location Listener */

    public class MyLocationListener implements LocationListener

    {


        public void onLocationChanged(Location loc) {
            current_lat = loc.getLatitude();
            current_lng = loc.getLongitude();

            if (flag == true) {
                flag = false;
                go(current_lat, current_lng);

            }
        }


        public void onProviderDisabled(String provider) {
            Toast.makeText(getApplicationContext(), "Gps Disabled !!! Please Enable It.",
                    Toast.LENGTH_SHORT).show();

        }


        public void onProviderEnabled(String provider)

        {
            Toast.makeText(getApplicationContext(), "Gps Enabled",
                    Toast.LENGTH_SHORT).show();

        }


        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

    }/* End of Class MyLocationListener */

    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;

    public void go(double c_lat, double c_lng) {
              System.out.println("your current Lat :: " + c_lat);    
              System.out.println("your current Lng :: " + c_lng);

             /*turn of gps now */ 
               turnGPSOff();
         }
    private void turnGPSOff() {
        String provider = Settings.Secure.getString(getContentResolver(),
                Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

        if (provider.contains("gps")) { // if gps is enabled
            final Intent poke = new Intent();
            poke.setClassName("com.android.settings",
                    "com.android.settings.widget.SettingsAppWidgetProvider");
            poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
            poke.setData(Uri.parse("3"));
            sendBroadcast(poke);
        }
    }

}/* End of UseGps Activity */