0

//单击按钮时,我通过使用位置侦听器获取纬度和经度。有时它会调用位置侦听器,有时不会。我必须重新启动电话才能正常工作。请提供最佳解决方案。

        mbtnCheckin.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            mrleMainCheckIn.setVisibility(View.GONE);
            mprogressBarCheckIn.setVisibility(View.VISIBLE);
            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            criteria.setAltitudeRequired(false);
            criteria.setBearingRequired(false);
            criteria.setCostAllowed(true);
            criteria.setPowerRequirement(Criteria.POWER_LOW);
            String serviceName = Context.LOCATION_SERVICE;
            mLocationManager = (LocationManager) getActivity().getSystemService(serviceName);

            mLocationListener = new LocationListener() {

                @Override
                public void onLocationChanged(Location location) {
                    // TODO Auto-generated method stub
                    Log.e(TAG, "onLocationChanged");
                    if (location != null) {
                        Log.e(TAG, "Current Latitude = " + location.getLatitude());
                        Log.e(TAG, "Current Longitude = " + location.getLongitude());   
                        mStrLatitue=Double.toString(location.getLatitude());
                        mStrLongitude=Double.toString(location.getLongitude());
                        if((WebServiceClient.isNetworkAvailable(getActivity()))){
                             new locationAsync().execute();

                            }else{
                                Toast.makeText(getActivity(), "Please check internet connection", Toast.LENGTH_LONG).show();
                            }
                    }
                    mLocationManager.removeUpdates(this);
                }



                @Override
                public void onProviderDisabled(String provider) {
                    // TODO Auto-generated method stub
                    Log.e(TAG, "onProviderDisabled");

                }

                @Override
                public void onProviderEnabled(String provider) {
                    // TODO Auto-generated method stub
                    Log.e(TAG, "onProviderEnabled");
                }

                @Override
                public void onStatusChanged(String provider, int status, Bundle extras) {
                    // TODO Auto-generated method stub
                    Log.e(TAG, "onStatusChanged");
                }
            };
            mLocationManager.removeUpdates(mLocationListener);
            if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 10, mLocationListener);
            } else {
                mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, mLocationListener);
            }
        }
        });

}
@Override
public void onDestroy() {
    mLocationManager.removeUpdates(mLocationListener);
    super.onDestroy();
}
4

1 回答 1

0

mLocationManager.removeUpdates(mLocationListener);从这部分代码中 删除:

mLocationManager.removeUpdates(mLocationListener); //REMOVE THIS LINE
if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
    mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 10, mLocationListener);
} else {
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, mLocationListener);
}  

您应该做的另一件事是在单击按钮mbtnCheckin后禁用按钮并等待获取位置以再次启用它。这可以防止在处理最后一个位置之前再次单击按钮

于 2013-10-31T13:58:24.513 回答