Latitude And Longitude Showing As 0 In Android
************* TrackGPS.java ***************************** import android.app.AlertDialog; import android.app.Service; import android.content.Context; import an
Solution 1:
Please manage required permission for marshmallow.
First you add this permission in manifest file
<uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION" /><uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION" />After in your activity First declare two variable like this,
privatestaticfinalintREQUEST_CODE_PERMISSION=1;
StringmPermission= Manifest.permission.ACCESS_FINE_LOCATION;
After in onCreate method
if(Build.VERSION.SDK_INT>= 23) {
if (checkSelfPermission(mPermission) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{mPermission,
},
REQUEST_CODE_PERMISSION);
return;
}
else
{
*here manage your code if permission already access
}
}
Solution 2:
Here is the background service to detect current location at time interval
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
@SuppressWarnings("MissingPermission")
publicclassLocationBackGroundServiceextendsServiceimplementsLocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
privatestatic final StringTAG = "LocationBackGroundService";
privatestatic final long INTERVAL = 10;
privatestatic final long FASTEST_INTERVAL = 10;
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
Location mCurrentLocation;
Context mCOntext;
publicvoidLocationBackGroundService(Context mContext) {
this.mCOntext = mContext;
}
protectedvoidcreateLocationRequest() {
mLocationRequest = newLocationRequest();
mLocationRequest.setInterval(INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
@OverridepublicvoidonStart(Intent intent, int startId) {
super.onStart(intent, startId);
mGoogleApiClient.connect();
}
@OverridepublicvoidonCreate() {
super.onCreate();
if (!isGooglePlayServicesAvailable()) {
}
createLocationRequest();
mGoogleApiClient = newGoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
if (mGoogleApiClient.isConnected()) {
startLocationUpdates();
}
}
privatebooleanisGooglePlayServicesAvailable() {
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (ConnectionResult.SUCCESS == status) {
returntrue;
} else {
returnfalse;
}
}
@Nullable@Overridepublic IBinder onBind(Intent intent) {
returnnull;
}
@OverridepublicvoidonConnected(Bundle bundle) {
startLocationUpdates();
}
@SuppressLint("LongLogTag")
protectedvoidstartLocationUpdates() {
PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
Log.d(TAG, "Location update started ..............: ");
}
@OverridepublicvoidonConnectionSuspended(int i) {
Toast.makeText(this,"OnConnection Suspended",Toast.LENGTH_SHORT).show();
}
@OverridepublicvoidonConnectionFailed(ConnectionResult connectionResult) {
Toast.makeText(this,"OnConnection Failed",Toast.LENGTH_SHORT).show();
}
@OverridepublicvoidonLocationChanged(Location location) {
if (null != mCurrentLocation) {
mCurrentLocation = location;
String lat = String.valueOf(mCurrentLocation.getLatitude());
String lng = String.valueOf(mCurrentLocation.getLongitude());
ConstantFunction.saveStatus(this, ConstantVariables.CURRENT_LATITUDE, lat);
ConstantFunction.saveStatus(this, ConstantVariables.CURRENT_LONGTITUDE, lng);
System.out.println("First Condition Hit");
System.out.println("Lat::-- " + lat + "\n" + "LONG::--" + lng);
}
System.out.println("Lat::-- " + location.getLatitude() + "\n" + "LONG::--" + location.getLongitude());
}
}
Put this code in seperate java file named LocationBackGroundService.java and call it in your MainActivity like this startService(new Intent(this, LocationBackGroundService.class)); and don't forget to add it in menifest like this <service android:name="LocationBackGroundService"></service>
Post a Comment for "Latitude And Longitude Showing As 0 In Android"