I know there are many questions that have been asked regarding this and i have been going through all that from couple of days but couldn't find a reasonable answer.I am newbie to android so i have no idea how things get done.
Basically I want to know how to run GPS as a service in background like when the application is installed it starts and how to compare that data received from gps background service with data in SQLite Data Base.
you can make service like above , this service get location from GPS and network provider and insert into database in background.
package com.example.alireza.locationtracker;
import android.Manifest;
import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.util.Log;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import java.util.Hashtable;
import java.util.Map;
public class LocationTracker extends Service {
public static LocationListener gpsloclistener;
public static LocationListener netlistener;
public static LocationManager mLocationManager;
public static Location gpsLOc;
public static Location netLOc;
private static Location gpslocation;
private static Location netlocation;
public static boolean isGPSEnabled;
public static boolean isNetworkEnabled;
@Override
public void onCreate() {
super.onCreate();
gpsloclistener = new gpsloclistener();
netlistener = new netlistener();
getLocation();
// myfunc();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void getLocation() {
try
{
mLocationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isGPSEnabled || isNetworkEnabled)
{
if (isGPSEnabled)
{
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,180000,0,gpsloclistener);
}
if (isNetworkEnabled)
{
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 0, netlistener);//aslan bar roye network nemishe mahdodiyat gozasht
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public class gpsloclistener implements LocationListener {
@Override
public void onLocationChanged(final Location location) {
if (location.getAccuracy() < 20) {
G.database.execSQL("INSERT INTO Gps_Location (Gps_lat,Gps_long,Gps_speed,Gps_time,Gps_bearing,Gps_alt,Gps_acur) VALUES ('" + location.getLatitude() + "','" + location.getLongitude() + "','" + location.getSpeed() + "','" + System.currentTimeMillis() + "','" + location.getBearing() + "','" + location.getAltitude() + "','" + location.getAccuracy() + "')");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
getLocation();
}
@Override
public void onProviderDisabled(String provider) {
}
}
private class netlistener implements LocationListener {
@Override
public void onLocationChanged(final Location location) {
G.database.execSQL("INSERT INTO Net_Location (Net_lat,Net_long,Net_speed,Net_time,Net_bearing,Net_alt,Net_acur) VALUES ('" + location.getLatitude() + "','" + location.getLongitude() + "','" + location.getSpeed() + "','" + System.currentTimeMillis() + "','" + location.getBearing() + "','" + location.getAltitude() + "','" + location.getAccuracy() + "')");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
getLocation();
}
@Override
public void onProviderDisabled(String provider) {
}
}
}
this service insert location to SQL lite database and you must start this service from somewhere like Main Activity
try{
startService(new Intent(getBaseContext(), LocationTracker.class));
}catch (Exception ex){
}
and to compare with SQL lite location, this fun calculate distance between to point
public static double distFrom(double lat1, double lng1, double lat2, double lng2) {
double earthRadius = 3958.75;
double dLat = Math.toRadians(lat2 - lat1);
double dLng = Math.toRadians(lng2 - lng1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLng / 2) * Math.sin(dLng / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double dist = earthRadius * c;
int meterConversion = 1609;
double v = dist * meterConversion;
return v;
}
And finally Define service to manifest
<service android:name=".LocationTracker"/>