Advertisement
Guest User

MyLocation.java

a guest
Feb 24th, 2014
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.27 KB | None | 0 0
  1. package com.example.android;
  2.  
  3. import java.util.Timer;
  4. import java.util.TimerTask;
  5.  
  6. import android.content.Context;
  7. import android.location.Location;
  8. import android.location.LocationListener;
  9. import android.location.LocationManager;
  10. import android.os.Bundle;
  11.  
  12. public class MyLocation {
  13.     Timer timer1;
  14.     LocationManager lm;
  15.     LocationResult locationResult;
  16.     boolean gps_enabled = false;
  17.     boolean network_enabled = false;
  18.    
  19.     public boolean getLocation(Context context, LocationResult result) {
  20.        
  21.         locationResult = result;
  22.         if (lm == null) {
  23.             lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
  24.         }
  25.        
  26.         try {
  27.             gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
  28.         } catch (Exception e) {}
  29.        
  30.         try {
  31.             network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  32.         } catch (Exception e) {}
  33.        
  34.         if (!gps_enabled && !network_enabled) {
  35.             return false;
  36.         }
  37.        
  38.         if (gps_enabled) {
  39.             lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
  40.         }
  41.        
  42.         if (network_enabled) {
  43.             lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
  44.         }
  45.        
  46.         timer1 = new Timer();
  47.         timer1.schedule(new GetLastLocation(), 10000);
  48.         return true;
  49.     }
  50.    
  51.    
  52.     LocationListener locationListenerGps = new LocationListener() {
  53.        
  54.         public void onStatusChanged(Location location) {
  55.             timer1.cancel();
  56.            
  57.             locationResult.gotLocation(location);
  58.             lm.removeUpdates(this);
  59.             lm.removeUpdates(locationListenerNetwork);
  60.         }
  61.         public void onProviderEnabled(String provider) {}
  62.         public void onProviderDisabled(String provider) {}
  63.         public void onLocationChanged(Location location) {}
  64.         public void onStatusChanged(String provider, int status, Bundle extras) {}
  65.     };
  66.    
  67.    
  68.     LocationListener locationListenerNetwork = new LocationListener() {
  69.        
  70.         public void onStatusChanged(Location location) {
  71.             timer1.cancel();
  72.             locationResult.gotLocation(location);
  73.             lm.removeUpdates(this);
  74.             lm.removeUpdates(locationListenerGps);
  75.         }
  76.         public void onProviderEnabled(String provider) {}
  77.         public void onProviderDisabled(String provider) {}
  78.         public void onLocationChanged(Location location) {}
  79.         public void onStatusChanged(String provider, int status, Bundle extras) {}
  80.     };
  81.    
  82.     class GetLastLocation extends TimerTask {
  83.         @Override
  84.         public void run() {
  85.             lm.removeUpdates(locationListenerGps);
  86.             lm.removeUpdates(locationListenerNetwork);
  87.            
  88.             Location netLoc = null;
  89.             Location gpsLoc = null;
  90.            
  91.             if (gps_enabled){
  92.                 gpsLoc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  93.             }
  94.             if (network_enabled) {
  95.                 netLoc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  96.             }
  97.            
  98.             if ((gpsLoc != null) && (netLoc != null)) {
  99.                 if (gpsLoc.getTime() > netLoc.getTime()) {
  100.                     locationResult.gotLocation(gpsLoc);
  101.                 } else {
  102.                     locationResult.gotLocation(netLoc);
  103.                 }
  104.                 return;
  105.             }
  106.            
  107.             if (gpsLoc != null) {
  108.                 locationResult.gotLocation(gpsLoc);
  109.                 return;
  110.             }
  111.            
  112.             if (netLoc != null) {
  113.                 locationResult.gotLocation(netLoc);
  114.                 return;
  115.             }
  116.            
  117.             locationResult.gotLocation(null);
  118.            
  119.         }
  120.     }
  121.    
  122.     public static abstract class LocationResult {
  123.         public abstract void gotLocation(Location location);
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement