Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.77 KB | None | 0 0
  1. package com.example.geosave;
  2.  
  3. import android.app.Service;
  4. import android.content.Intent;
  5. import android.location.Location;
  6. import android.location.LocationListener;
  7. import android.location.LocationManager;
  8. import android.os.Bundle;
  9. import android.os.IBinder;
  10. import android.content.Context;
  11. import android.util.Log;
  12.  
  13. public class GPSService extends Service implements LocationListener {
  14.  
  15.     /*Adresse et la position d'une alerte*/
  16.     private String address;
  17.     private Coordinates coords;
  18.     private static final long MIN_DISTANCE = 10;
  19.     private static final long MIN_TIME = 5000;
  20.  
  21.     private final Context mContext; //TODO : se renseigner sur le context dans Android
  22.  
  23.     /*Variable qui permettent de savoir comment on va obtenir nos coordonnées*/
  24.     boolean isGPSEnable = false;
  25.     boolean isNetworkEnable = false;
  26.     boolean canGetLocation = false;
  27.  
  28.     Location location;
  29.     double longitude;
  30.     double latitude;
  31.  
  32.     protected LocationManager locationManager;
  33.  
  34.     public GPSService(Context context) {
  35.         this.mContext = context;
  36.         getLocation();
  37.     }
  38.  
  39.     public void getLocation(){
  40.         locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
  41.  
  42.         /*On regarde si on a les coordonnées via le netword ou le gps*/
  43.         isGPSEnable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
  44.         isNetworkEnable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  45.  
  46.         if(!isGPSEnable && !isNetworkEnable){
  47.             //TODO : message disant que l'utilisateur doit activer wifi ou localisation
  48.         }
  49.         else{
  50.             this.canGetLocation = true;
  51.  
  52.             /*On essaye de récupérer les coordonnées GPS via le réseau*/
  53.             if(isNetworkEnable){
  54.                 locationManager.requestLocationUpdates(locationManager.NETWORK_PROVIDER,MIN_TIME,MIN_DISTANCE,this);
  55.                 //Log.d("tag","message");
  56.                 if(locationManager != null){
  57.                     location = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);
  58.                 }
  59.                 if(location != null){
  60.                     this.latitude = location.getLatitude();
  61.                     this.longitude = location.getLongitude();
  62.                 }
  63.             }
  64.  
  65.             /*On essaye de récupérer les coordonnées GPS via le service GPS*/
  66.             if(isGPSEnable){
  67.                 if(location == null){
  68.                     locationManager.requestLocationUpdates(locationManager.NETWORK_PROVIDER,MIN_TIME,MIN_DISTANCE,this);
  69.                     //Log.d("tag","message");
  70.                     if(locationManager != null){
  71.                         location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
  72.                         if(location != null){
  73.                             this.latitude = location.getLatitude();
  74.                             this.longitude = location.getLongitude();
  75.                             Log.v("POSITION", Double.toString(latitude));
  76.  
  77.                         }
  78.                     }
  79.                 }
  80.             }
  81.         }
  82.     }
  83.  
  84.     public void onCreate(){
  85.         getLocation();
  86.     }
  87.  
  88.     public void onDestroy(){
  89.  
  90.     }
  91.  
  92.     @Override
  93.     public IBinder onBind(Intent intent) {
  94.         // TODO: Return the communication channel to the service.
  95.         throw new UnsupportedOperationException("Not yet implemented");
  96.     }
  97.  
  98.     @Override
  99.     public void onLocationChanged(Location location) {
  100.  
  101.     }
  102.  
  103.     @Override
  104.     public void onStatusChanged(String provider, int status, Bundle extras) {
  105.  
  106.     }
  107.  
  108.     @Override
  109.     public void onProviderEnabled(String provider) {
  110.  
  111.     }
  112.  
  113.     @Override
  114.     public void onProviderDisabled(String provider) {
  115.  
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement