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