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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
| /***************************************************************************************************
La présente activité (2) est en réalité un service qui permet d'effectuer des captures de
géolocalisations en arrière plan tant que l'application (1) ne lui a pas donné l'ordre de
s'arrêter.
***************************************************************************************************/
package loupapet.test;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.*;
import android.os.* ;
import android.os.Bundle;
import android.os.IBinder;
/**************************************************************************************************
L'activité (2)
**************************************************************************************************/
public class Activite2 extends Service
{
private LocationManager lm = null ;
private double latitude ;
private double longitude ;
private double altitude ;
private float precision ;
private SharedPreferences donnees ;
//=================================================================================================
// *** Les méthodes de contrôle du service ***
//=================================================================================================
@Override
public IBinder onBind(Intent arg0) // Attachement du service
{
System.out.println( "Activité (2) : " + "Service attaché (onBind)") ;
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) // Démarrage du service
{
System.out.println ("Activité (2) : " + "Service démarré (onStartCommand)") ;
return START_STICKY ; // Le service continue même si l'activité qui l'a lancée est détruite
}
@Override
public void onDestroy() // destruction du service
{ System.out.println ("Activité (2) : " + "détruite (onDestroy)") ;
if(lm != null)
{ lm.removeUpdates(ll) ;
System.out.println (" " + "mises à jour supprimées") ;
}
lm = null;
}
@Override
public void onRebind(Intent intent) // reattachement du service
{
System.out.println ("Activité (2) : " + "réattachée (onRebind)") ;
}
public void onCreate() // Création du service
{
System.out.println ("Activité (2) : " + "Service créé (onCreate)") ;
donnees = getSharedPreferences( "Activité"
, Context.MODE_MULTI_PROCESS
) ;
//*** Lancemant de la géolocalisation :
lm = (LocationManager) this.getSystemService(LOCATION_SERVICE) ;
if(lm != null) // pour propreté au cas où
{ lm.removeUpdates(ll) ;
System.out.println (" " + "mises à jour supprimées") ;
}
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, ll) ;
}
//=================================================================================================
// *** Les méthodes de traitement de la géolocalisation ***
//=================================================================================================
private LocationListener ll = new LocationListener()
{
public void onLocationChanged(Location geoloc)
{
latitude = geoloc.getLatitude() ;
longitude = geoloc.getLongitude() ;
altitude = geoloc.getAltitude() ;
precision = geoloc.getAccuracy() ;
System.out.println ( "Activité (2) :\n"
+ " - longitude=" + longitude + "\n"
+ " - latitude=" + latitude + "\n"
+ " - altitude=" + altitude + "\n"
+ " - précision=" + precision
) ;
//*** On dépose les données de géolocalisations pour être affichées dans l'activité (1) :
donnees.edit().putString("longitude",String.format("%.5f",longitude))
.putString("latitude" ,String.format("%.5f",latitude ))
.putString("altitude" ,String.format("%.1f",altitude ))
.putString("precision",String.format("%.1f",precision))
.commit();
}
public void onProviderEnabled(String provider)
{
System.out.println ( "Activité (2) :\n"
+ " - " + provider + " est maintenant actif (Enabled)"
) ;
}
public void onProviderDisabled(String provider)
{
System.out.println ( "Activité (2) :\n"
+ " - " + provider + " est maintenant inactif (Disabled)"
) ;
}
public void onStatusChanged(String provider, int statut, Bundle extras)
{
String etat = "" ;
switch (statut)
{ case LocationProvider.OUT_OF_SERVICE : etat = "HORS_SERVICE" ; break ;
case LocationProvider.TEMPORARILY_UNAVAILABLE : etat = "TEMPORAIREMENT_INVALIDE" ; break ;
case LocationProvider.AVAILABLE : etat = "VALIDE" ; break;
}
System.out.println ( "Activité (2) :\n"
+ " - " + provider + " a maintenant le status " + etat ) ;
}
} ;
} |
Partager