1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.support.annotation.Nullable;
//import com.google.android.gms.location.LocationListener;
import android.location.LocationListener;
public class GPS_Service extends Service {
private LocationListener listener;
private LocationManager locationManager;
//private LocationListener locationListener;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate() {
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
listener = new LocationListener() {
boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
//function qui renvoie la latitude et la longitude de notre position sous forme de text
@Override
public void onLocationChanged(Location location) {
Intent localisation = new Intent("location_update");
localisation.putExtra("coordinates", location.getLongitude() + " " + location.getLatitude());
//On envoie notre intent grâce à la fonction "sendBroadcast"
sendBroadcast(localisation);
}
//@Override
public void onStatusChanged(String s, int i, Bundle extras) {
}
//@Override
public void onProviderEnabled(String s) {
}
//@Override
public void onProviderDisabled(String s) {
Intent localisation = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
localisation.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(localisation);
}
/**
* //On envoie nos coordonnées Longitude et Latitude à notre map
Intent IntentLocalisation = new Intent("location_update");
IntentLocalisation.putExtra("coordonnées :", listener.onLocationChanged().getLongitude() + " " + listener.onLocationChanged().getLatitude());
//On envoie notre intent grâce à la fonction "sendBroadcast"
sendBroadcast(IntentLocalisation);**/
};
/**
//On envoie nos coordonnées Longitude et Latitude à notre map
Intent IntentLocalisation = new Intent("location_update");
IntentLocalisation.putExtra("coordonnées :", listener.onLocationChanged().getLongitude() + " " + listener.onLocationChanged().getLatitude());
//On envoie notre intent grâce à la fonction "sendBroadcast"
sendBroadcast(IntentLocalisation);**/
locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
//noinspection MissingPermission
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,0,listener);
}
@Override
public void onDestroy() {
super.onDestroy();
if(locationManager != null){
//noinspection MissingPermission
locationManager.removeUpdates(listener);
}
}
} |
Partager