Background: I am now working on an Android app to track vehicles where I required to get device location on every 10 second as on management decision. We are already developed this app which provides us updated location on time as our expected time interval.
Problem: When Vehicles are driving on the roads, the app gives us correct coordinates as our expected time interval, but sometime 5-20 minutes it provide same coordinates yet the vehicle is in a different location.
Code Review: 1) We use a method named getLocation() inside LocationClass that provide us latitude longitude of a device current location.
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 5;
private static final long MIN_TIME_BW_UPDATES = 1000*20;
public Location getLocation(Context context) {
Location location=null;
try {
locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(context,"Please! Provide Location Access Permission.....", Toast.LENGTH_LONG).show();
return TODO;
}
// if GPS Enabled get lat/long using GPS Services
if(isGPSEnabled) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
location.setProvider("GPS");
}
}
}
if (isNetworkEnabled && location==null) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
location.setProvider("AGPS");
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
2) We repeat this method using a timer on a service named GPSTracker at fixed time interval.
public class GPSTracker extends Service implements LocationListener {
private Timer mTimer = null;
private Handler mHandler = new Handler();
Location location;
@Override
public void onCreate() {
super.onCreate();
if (mTimer != null) {
mTimer.cancel();
} else {
// recreate new
mTimer = new Timer();
}
// schedule task
mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 10 * 1000, 10 * 1000);
}
class TimeDisplayTimerTask extends TimerTask {
@Override
public void run() {
// run on another thread
mHandler.post(new Runnable() {
@Override
public void run() {
location=new Location("");
LocationCls cls = new LocationCls();
location = cls.getLocation(getBaseContext());
}
});
}
}
}
I am trying to solve this issue a 2 long day but not found any coding problem or solution yet. Any Solution or Suggestion is highly appreciate.
Thanks in advance....