I am working on the application in which I need to store the location in the database after every 1 minute for that particular device/user.
Suppose if user is moving while the application is open then his coordinates should keep on getting inserted into database after every 1 minute. So for that I have my Web Services setup which can insert the data into the MySql Database. I have tested my WebService in the insertion part, it works fine.
But the problem that I am having is- I don’t think so it is inserting into MySql database for that particular device/user after every 1 minute. Can anyone tell me why it is not happening after every 1 minute? And if there is any problem in the below code, can anyone help me out here by providing some example which will work as per my scenario above?
Any thoughts will be of great help.
private final String id = UUID.randomUUID().toString();
private final String sampleURL = ServerInfo.getServiceURL() + "/insert";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new GPSLocationListener();
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
60000,
0,
this.locationListener);
}
Below is the class that implements LocationListener.
private class GPSLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
GeoPoint point = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
}
// Below code is used to insert into database
String userId = id;
String mLatitude = Double.toString(location.getLatitude());
String mLongitude = Double.toString(location.getLongitude());
String mProvider = location.getProvider();
String mHasAltitude = Boolean.toString(location.hasAltitude());
String mAltitude = Double.toString(location.getAltitude());
String mHasSpeed = Boolean.toString(location.hasSpeed());
String mSpeed = Float.toString(location.getSpeed());
String mHasBearing = Boolean.toString(location.hasBearing());
String mBearing = Float.toString(location.getBearing());
String mHasAccuracy = Boolean.toString(location.hasAccuracy());
String mAccuracy = Float.toString(location.getAccuracy());
WebServiceTask wst = new WebServiceTask(WebServiceTask.POST_TASK,
getApplicationContext(), "Posting data...");
wst.addNameValuePair("user_id", userId);
wst.addNameValuePair("mlatitude", mLatitude);
wst.addNameValuePair("mlogitude", mLongitude);
wst.addNameValuePair("mprovider", mProvider);
wst.addNameValuePair("mhasAltitude", mHasAltitude);
wst.addNameValuePair("mAltitude", mAltitude);
wst.addNameValuePair("mhasspeed", mHasSpeed);
wst.addNameValuePair("mspeed", mSpeed);
wst.addNameValuePair("mhasbearing", mHasBearing);
wst.addNameValuePair("mbearing", mBearing);
wst.addNameValuePair("mhasaccuracy", mHasAccuracy);
wst.addNameValuePair("maccuracy", mAccuracy);
// This will insert into database
wst.execute(new String[] { sampleURL });
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
NOTE:- It is inserting into Database but it’s not inserting at exactly or approximately in the time interval I need which is of 1 minute
This problem may be caused by several issues which are not directly under your control as a programmer. Some that come to mind are
The network may not be available and therefore no positioning data can be sent to your app.
An ill-behaved app is taking up CPU time and the OS doesn’t interrupt it within your desired time interval.
In general, getting exact timing on a device designed for multi-tasking is nearly impossible. There can also be issues with getting data at “approximate” time intervals depending on what margin of error you can accept.