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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
|
package com.donnee.service;
import java.lang.reflect.Method;
import java.util.Calendar;
import com.locartion.service.R;
import com.objet.metier.Coordonnees;
import com.objet.metier.MetierDao;
import android.app.AlarmManager;
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.os.SystemClock;
import android.provider.Settings;
import android.widget.Toast;
public class ReceiveData extends IntentService {
public ReceiveData(String name) {
super(name);
// TODO Auto-generated constructor stub
}
private LocationManager mLocationManager = null;
private LocationListener mLocationListener = null;
private Location currentBestLocation = null;
private static final int TWO_MINUTES = 1000 * 60 * 2;
public static final String STOP_SERVICE = "STOP_SERVICE";
public static final String START_SERVICE = "START_SERVICE";
public static final int MIN = 1;
public static final int MINDIST = 1;
private Bundle sat;
private Double latitude ;
private Double longitude ;
private Double altitude ;
private Float precision ;
private Float vitesse ;
private String fournisseur ;
private String date ;
private Float direction ;
private long GPS_interval = 0 ;
private long GPS_mindistance = 0 ;
NotificationManager mNM;
PendingIntent proccess ;
// private Handler handler = new Handler();
@Override
public IBinder onBind(Intent intent) {
return null ;
}
@Override
public void onCreate() {
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, GPS_interval , GPS_mindistance, mLocationListener);
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, GPS_interval , GPS_mindistance , mLocationListener);
Processus thread = new Processus() ;
}
/*
*
*/
private boolean getGPSStatus()
{
String allowedLocationProviders = Settings.System.getString(getContentResolver(),Settings.System.LOCATION_PROVIDERS_ALLOWED);
if (allowedLocationProviders == null) {
allowedLocationProviders = "";
}
return allowedLocationProviders.contains(LocationManager.GPS_PROVIDER);
}
private void setGPSStatus(boolean pNewGPSStatus){
String allowedLocationProviders = Settings.System.getString(getContentResolver(),Settings.System.LOCATION_PROVIDERS_ALLOWED);
if (allowedLocationProviders == null) {
allowedLocationProviders = "";
}
boolean networkProviderStatus = allowedLocationProviders.contains(LocationManager.NETWORK_PROVIDER);
allowedLocationProviders = "";
if (networkProviderStatus == true) {
allowedLocationProviders += LocationManager.NETWORK_PROVIDER;
}
if (pNewGPSStatus == true) {
allowedLocationProviders += "," + LocationManager.GPS_PROVIDER;
}
Settings.System.putString(getContentResolver(),
Settings.System.LOCATION_PROVIDERS_ALLOWED, allowedLocationProviders);
try {
Method m = mLocationManager.getClass().getMethod("updateProviders", new Class[] {});
m.setAccessible(true);
m.invoke(mLocationManager, new Object[]{});
}
catch(Exception e) {
e.getMessage() ;
}
return;
}
/**
* Sends the location to the user.
* @param location the location to send.
*/
public void sendLocationUpdate(Location location) {
latitude = this.setLatitude(location.getLatitude());
longitude = this.setLongitude(location.getLongitude());
altitude = this.setAltitude(location.getAltitude());
precision = this.setPrecision(location.getAccuracy());
vitesse = this.setVitesse(location.getSpeed());
fournisseur = this.setFournisseur(location.getProvider());
direction = this.setDirection(location.getBearing());
date = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
Toast.makeText(ReceiveData.this, "Voici les coordonnées de votre téléphone : " + latitude + " " + longitude + " " + altitude + " " + date + " " , Toast.LENGTH_LONG).show();
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public void onStartCommand(final Intent intent, int startId) {
super.onStart(intent, startId);
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
if ( intent.getAction().equals(STOP_SERVICE) ) {
destroy();
stopSelf();
return;
}
try {
if(!getGPSStatus())
setGPSStatus(true);
}
catch(Exception e) {
e.getMessage() ;
}
mLocationListener = new LocationListener() {
public void onLocationChanged(Location location) {
{
if (isBetterLocation(location, currentBestLocation)) {
currentBestLocation = location;
sendLocationUpdate(currentBestLocation);
}
}
}
public void onStatusChanged(String provider, int status, Bundle arg2) {
String newStatus = "";
switch (status) {
case LocationProvider.OUT_OF_SERVICE: newStatus = "OUT_OF_SERVICE" ;
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE: newStatus = "TEMPORARILY_UNAVAILABLE";
break;
case LocationProvider.AVAILABLE: newStatus = "AVAILABLE";
break;
}
String msg = String.format(getResources().getString(R.string.new_location), provider, newStatus);
Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
}
public void onProviderDisabled(String provider) {
String msg = String.format(getResources().getString(R.string.provider_disabled), provider);
Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
}
public void onProviderEnabled(String provider) {
String msg = String.format(getResources().getString(R.string.provider_enabled), provider);
Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
}
};
// We query every available location providers
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN , MINDIST , mLocationListener);
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER , MIN , MINDIST , mLocationListener);
Location location = mLocationManager.getLastKnownLocation("gps");
if (location == null)
{
location = mLocationManager.getLastKnownLocation("network");
if (location != null) {
if (isBetterLocation(location, currentBestLocation)) {
currentBestLocation = location;
sendLocationUpdate(currentBestLocation);
}
}
}
}
public void onDestroy() {
destroy();
}
private void destroy() {
if (mLocationManager != null && mLocationListener != null) {
mLocationManager.removeUpdates(mLocationListener);
mLocationManager = null;
mLocationListener = null;
}
}
/** From the SDK documentation. Determines whether one Location reading is better than the current Location fix
* @param location The new Location that you want to evaluate
* @param currentBestLocation The current Location fix, to which you want to compare the new one
*/
protected boolean isBetterLocation(Location location, Location currentBestLocation) {
if (currentBestLocation == null) {
// A new location is always better than no location
return true;
}
// Check whether the new location fix is newer or older
long timeDelta = location.getTime() - currentBestLocation.getTime();
boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
boolean isNewer = timeDelta > 0;
// If it's been more than two minutes since the current location, use the new location
// because the user has likely moved
if (isSignificantlyNewer) {
return true;
// If the new location is more than two minutes older, it must be worse
} else if (isSignificantlyOlder) {
return false;
}
// Check whether the new location fix is more or less accurate
int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
boolean isLessAccurate = accuracyDelta > 0;
boolean isMoreAccurate = accuracyDelta < 0;
boolean isSignificantlyLessAccurate = accuracyDelta > 200;
// Check if the old and new location are from the same provider
boolean isFromSameProvider = isSameProvider(location.getProvider(),
currentBestLocation.getProvider());
// Determine location quality using a combination of timeliness and accuracy
if (isMoreAccurate) {
return true;
} else if (isNewer && !isLessAccurate) {
return true;
} else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
return true;
}
return false;
}
/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
if (provider1 == null) {
return provider2 == null;
}
return provider1.equals(provider2);
}
/**
* @return the sat
*/
public Bundle getSat() {
return sat;
}
/**
* @param sat the sat to set
*/
public void setSat(Bundle sat) {
this.sat = sat;
}
/**
* @return the latitude
*/
public Double getLatitude() {
return latitude;
}
/**
* @param latitude the latitude to set
* @return
*/
public Double setLatitude(Double latitude) {
return this.latitude = latitude;
}
/**
* @return the longitude
*/
public Double getLongitude() {
return longitude;
}
/**
* @param longitude the longitude to set
* @return
*/
public Double setLongitude(Double longitude) {
return this.longitude = longitude;
}
/**
* @return the altitude
*/
public Double getAltitude() {
return altitude;
}
/**
* @param altitude the altitude to set
* @return
*/
public Double setAltitude(Double altitude) {
return this.altitude = altitude;
}
/**
* @return the precision
*/
public Float getPrecision() {
return precision;
}
/**
* @param precision the precision to set
* @return
*/
public Float setPrecision(Float precision) {
return this.precision = precision;
}
/**
* @return the vitesse
*/
public Float getVitesse() {
return vitesse;
}
/**
* @param vitesse the vitesse to set
* @return
*/
public Float setVitesse(Float vitesse) {
return this.vitesse = vitesse;
}
/**
* @return the fournisseur
*/
public String getFournisseur() {
return fournisseur;
}
/**
* @param fournisseur the fournisseur to set
* @return
*/
public String setFournisseur(String fournisseur) {
return this.fournisseur = fournisseur;
}
/**
* @return the direction
*/
public Float getDirection() {
return direction;
}
/**
* @param direction the direction to set
* @return
*/
public Float setDirection(Float direction) {
return this.direction = direction;
}
private class Processus extends AsyncTask<Void, Void , Void>{
PendingIntent proccess ;
private long firstTime;
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(getApplicationContext(), "Début du traitement asynchrone", Toast.LENGTH_LONG).show();
}
@Override
protected void onProgressUpdate(Void... values){
super.onProgressUpdate(values);
}
@Override
protected Void doInBackground(Void...arg0) {
try {
firstTime = SystemClock.elapsedRealtime();
String name = "Service" ;
ReceiveData reception = new ReceiveData(name);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, 1000 * 60 * 2, proccess);
Coordonnees cord = new Coordonnees(reception.getLatitude(), reception.getLongitude(), reception.getAltitude(), reception.getPrecision(), reception.getVitesse(), reception.getDirection(), reception.getDate(), reception.getFournisseur());
MetierDao metier = new MetierDao(getApplicationContext());
metier.ajouter(cord);
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null ;
}
@Override
protected void onPostExecute(Void result) {
// And cancel the alarm.
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.cancel(proccess);
Toast.makeText(getApplicationContext(), "Le traitement asynchrone est terminé", Toast.LENGTH_LONG).show();
}
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
}
} |
Partager