all i am trying to get the location of the device but it always returns null. I know for the first time it will return null but again and again it is returning null. Below is my code. Please help me to solve this .
public class LocationHelper implements LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { private static final String TAG = "LocationService"; private static final long INTERVAL = 1000 * 5; private static final long FASTEST_INTERVAL = 1000 * 3; Context context; LocationRequest mLocationRequest; GoogleApiClient mGoogleApiClient; Location mCurrentLocation; String mLastUpdateTime; LocationUpdateListener locationUpdateListener; public LocationHelper(Context context, LocationUpdateListener locationUpdateListener) { this.context = context; this.locationUpdateListener = locationUpdateListener; if (!isGooglePlayServicesAvailable()) { } else { createLocationRequest(); mGoogleApiClient = new GoogleApiClient.Builder(context) .addApi(LocationServices.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); mGoogleApiClient.connect(); } } /** * Create the location request */ protected void createLocationRequest() { mLocationRequest = new LocationRequest(); mLocationRequest.setInterval(INTERVAL); mLocationRequest.setFastestInterval(FASTEST_INTERVAL); mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); } private boolean isGooglePlayServicesAvailable() { int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context); if (ConnectionResult.SUCCESS == status) { return true; }else if(status == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED){ Log.d(TAG, "please udpate your google play service"); Log.d(TAG, "please udpate your google play service"); Log.d(TAG, "please udpate your google play service"); return true; }else { // GooglePlayServicesUtil.getErrorDialog(status, getBaseContext(), 0).show(); Log.d(TAG, "google play services is not available - ...............:"); return false; } } @Override public void onConnected(Bundle bundle) { startLocationUpdates(); } private void startLocationUpdates() { mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); mLastUpdateTime = DateFormat.getTimeInstance().format(new Date()); Log.d(TAG, "Location update started ..............: "); fetchLocation(); Log.d(TAG, "Location update started ..............: "); Log.d(TAG, "Location update started ..............: "); } @Override public void onConnectionSuspended(int i) { } @Override public void onConnectionFailed(ConnectionResult connectionResult) { } @Override public void onLocationChanged(Location location) { mCurrentLocation = location; mLastUpdateTime = DateFormat.getTimeInstance().format(new Date()); fetchLocation(); Log.v("onLocationChanged","onLocationChanged"); Log.v("onLocationChanged","onLocationChanged"); Log.v("onLocationChanged","onLocationChanged"); } //Get the location private void fetchLocation(){ while (mCurrentLocation == null) { try { mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); Log.d(TAG, "Location null "); Thread.sleep(2000); } catch (Exception ex) { ex.printStackTrace(); } } if (mCurrentLocation != null) { locationUpdateListener.locationUpdate(mCurrentLocation); } } }
Thanks in advance