Negrox

test2

Jun 25th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.64 KB | None | 0 0
  1. public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
  2.     Timer timer1;
  3.     LocationManager lm;
  4.     LocationResult locationResult;
  5.     boolean gps_enabled=false;
  6.     boolean network_enabled=false;
  7.  
  8.     private GoogleMap mMap;
  9.     private FusedLocationProviderClient mFusedLocationClient;
  10.  
  11.     public void checkPermission() {
  12.         if (PermissionChecker.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ||
  13.                 PermissionChecker.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
  14.                 ) {//Can add more as per requirement
  15.  
  16.             ActivityCompat.requestPermissions(this,
  17.                     new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
  18.                     123);
  19.         }
  20.     }
  21.  
  22.     public boolean getLocation(Context context, LocationResult result)
  23.     {
  24.         if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
  25.             checkPermission();
  26.         }
  27.         //I use LocationResult callback class to pass location value from MyLocation to user code.
  28.         locationResult=result;
  29.         if(lm==null)
  30.             lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
  31.  
  32.         //exceptions will be thrown if provider is not permitted.
  33.         try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
  34.         try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}
  35.  
  36.         //don't start listeners if no provider is enabled
  37.         if(!gps_enabled && !network_enabled)
  38.             return false;
  39.  
  40.         if(gps_enabled)
  41.             lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
  42.         if(network_enabled)
  43.             lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
  44.         timer1=new Timer();
  45.         timer1.schedule(new GetLastLocation(), 20000);
  46.         return true;
  47.     }
  48.  
  49.     LocationListener locationListenerGps = new LocationListener() {
  50.         public void onLocationChanged(Location location) {
  51.             timer1.cancel();
  52.             locationResult.gotLocation(location);
  53.             lm.removeUpdates(this);
  54.             lm.removeUpdates(locationListenerNetwork);
  55.         }
  56.         public void onProviderDisabled(String provider) {}
  57.         public void onProviderEnabled(String provider) {}
  58.         public void onStatusChanged(String provider, int status, Bundle extras) {}
  59.     };
  60.  
  61.     LocationListener locationListenerNetwork = new LocationListener() {
  62.         public void onLocationChanged(Location location) {
  63.             timer1.cancel();
  64.             locationResult.gotLocation(location);
  65.             lm.removeUpdates(this);
  66.             lm.removeUpdates(locationListenerGps);
  67.         }
  68.         public void onProviderDisabled(String provider) {}
  69.         public void onProviderEnabled(String provider) {}
  70.         public void onStatusChanged(String provider, int status, Bundle extras) {}
  71.     };
  72.  
  73.     class GetLastLocation extends TimerTask {
  74.         @Override
  75.         public void run() {
  76.             if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
  77.                 checkPermission();
  78.             }
  79.             lm.removeUpdates(locationListenerGps);
  80.             lm.removeUpdates(locationListenerNetwork);
  81.  
  82.             Location net_loc=null, gps_loc=null;
  83.             if(gps_enabled)
  84.                 gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  85.             if(network_enabled)
  86.                 net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  87.  
  88.             //if there are both values use the latest one
  89.             if(gps_loc!=null && net_loc!=null){
  90.                 if(gps_loc.getTime()>net_loc.getTime())
  91.                     locationResult.gotLocation(gps_loc);
  92.                 else
  93.                     locationResult.gotLocation(net_loc);
  94.                 return;
  95.             }
  96.  
  97.             if(gps_loc!=null){
  98.                 locationResult.gotLocation(gps_loc);
  99.                 return;
  100.             }
  101.             if(net_loc!=null){
  102.                 locationResult.gotLocation(net_loc);
  103.                 return;
  104.             }
  105.             locationResult.gotLocation(null);
  106.         }
  107.     }
  108.  
  109.     public static abstract class LocationResult{
  110.         public abstract void gotLocation(Location location);
  111.     }
  112.  
  113.  
  114.     @Override
  115.     protected void onCreate(Bundle savedInstanceState) {
  116.         super.onCreate(savedInstanceState);
  117.  
  118.         if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
  119.             checkPermission();
  120.         }
  121.  
  122.         if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M)
  123.  
  124.             setContentView(R.layout.activity_maps);
  125.         // Obtain the SupportMapFragment and get notified when the map is ready to be used.
  126.         SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  127.                 .findFragmentById(R.id.map);
  128.         mapFragment.getMapAsync(this);
  129.  
  130.  
  131.  
  132.         mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
  133.         mFusedLocationClient.getLastLocation()
  134.                 .addOnSuccessListener(this, new OnSuccessListener<Location>() {
  135.                     @Override
  136.                     public void onSuccess(Location location) {
  137.                         // Got last known location. In some rare situations this can be null.
  138.                         if (location != null) {
  139.                             // ...
  140.                         }
  141.                     }
  142.                 });
  143.  
  144.  
  145.     }
  146.  
  147. }
Add Comment
Please, Sign In to add comment