位置监听器没有响应。它还依赖于操作系统吗?我必须为此重述手机

时间:2013-10-31 13:17:21

标签: android locationlistener

//按钮点击我通过使用位置监听器获取纬度和经度。有一段时间它没有称为位置监听器。我必须重新打电话然后才能正常工作。请提供最佳解决方案。

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

1 个答案:

答案 0 :(得分: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后单击它并等待获取的位置再次启用它。这样可以防止在处理最后一个位置之前再次单击该按钮