Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.geosave;
- 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.IBinder;
- import android.content.Context;
- import android.util.Log;
- public class GPSService extends Service implements LocationListener {
- /*Adresse et la position d'une alerte*/
- private String address;
- private Coordinates coords;
- private static final long MIN_DISTANCE = 10;
- private static final long MIN_TIME = 5000;
- private final Context mContext; //TODO : se renseigner sur le context dans Android
- /*Variable qui permettent de savoir comment on va obtenir nos coordonnées*/
- boolean isGPSEnable = false;
- boolean isNetworkEnable = false;
- boolean canGetLocation = false;
- Location location;
- double longitude;
- double latitude;
- protected LocationManager locationManager;
- public GPSService(Context context) {
- this.mContext = context;
- getLocation();
- }
- public void getLocation(){
- locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
- /*On regarde si on a les coordonnées via le netword ou le gps*/
- isGPSEnable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
- isNetworkEnable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
- if(!isGPSEnable && !isNetworkEnable){
- //TODO : message disant que l'utilisateur doit activer wifi ou localisation
- }
- else{
- this.canGetLocation = true;
- /*On essaye de récupérer les coordonnées GPS via le réseau*/
- if(isNetworkEnable){
- locationManager.requestLocationUpdates(locationManager.NETWORK_PROVIDER,MIN_TIME,MIN_DISTANCE,this);
- //Log.d("tag","message");
- if(locationManager != null){
- location = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);
- }
- if(location != null){
- this.latitude = location.getLatitude();
- this.longitude = location.getLongitude();
- }
- }
- /*On essaye de récupérer les coordonnées GPS via le service GPS*/
- if(isGPSEnable){
- if(location == null){
- locationManager.requestLocationUpdates(locationManager.NETWORK_PROVIDER,MIN_TIME,MIN_DISTANCE,this);
- //Log.d("tag","message");
- if(locationManager != null){
- location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
- if(location != null){
- this.latitude = location.getLatitude();
- this.longitude = location.getLongitude();
- Log.v("POSITION", Double.toString(latitude));
- }
- }
- }
- }
- }
- }
- public void onCreate(){
- getLocation();
- }
- public void onDestroy(){
- }
- @Override
- public IBinder onBind(Intent intent) {
- // TODO: Return the communication channel to the service.
- throw new UnsupportedOperationException("Not yet implemented");
- }
- @Override
- public void onLocationChanged(Location location) {
- }
- @Override
- public void onStatusChanged(String provider, int status, Bundle extras) {
- }
- @Override
- public void onProviderEnabled(String provider) {
- }
- @Override
- public void onProviderDisabled(String provider) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement