Advertisement
Guest User

Untitled

a guest
May 2nd, 2015
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.27 KB | None | 0 0
  1. package com.example.user.test;
  2.  
  3.  
  4. import android.app.AlertDialog;
  5. import android.app.Service;
  6. import android.content.Context;
  7. import android.content.DialogInterface;
  8. import android.content.Intent;
  9. import android.location.Location;
  10. import android.location.LocationListener;
  11. import android.location.LocationManager;
  12. import android.os.Bundle;
  13. import android.os.IBinder;
  14. import android.provider.Settings;
  15. import android.util.Log;
  16. import android.widget.TextView;
  17.  
  18.  
  19. public class GPSTracker extends Service implements LocationListener{
  20.  
  21.     private final Context context;
  22.  
  23.  
  24.     boolean isGPSEnabled = false;
  25.     boolean isNetworkEnabled = false;
  26.     boolean canGetLocation = false;
  27.  
  28.     Location location;
  29.  
  30.     double latitude;
  31.     double longitude;
  32.  
  33.     private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1;
  34.     private static final long MIN_TIME_BW_UPDATES = 0;
  35.  
  36.     protected LocationManager locationManager;
  37.  
  38.     public GPSTracker(Context context,TextView tv) {
  39.         this.context = context;
  40.  
  41.     }
  42.  
  43.  
  44.     public Location getLocation() {
  45.         try {
  46.             locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
  47.  
  48.             isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
  49.  
  50.             isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  51.  
  52.             if(!isGPSEnabled && !isNetworkEnabled) {
  53.  
  54.             } else {
  55.                 this.canGetLocation = true;
  56.  
  57.                 if (isNetworkEnabled) {
  58.  
  59.                     locationManager.requestLocationUpdates(
  60.                             LocationManager.NETWORK_PROVIDER,
  61.                             MIN_TIME_BW_UPDATES,
  62.                             MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
  63.  
  64.                     if (locationManager != null) {
  65.                         location = locationManager
  66.                                 .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  67.  
  68.                         if (location != null) {
  69.  
  70.                             latitude = location.getLatitude();
  71.                             longitude = location.getLongitude();
  72.                         }
  73.                     }
  74.  
  75.                 }
  76.  
  77.                 if(isGPSEnabled) {
  78.                     if(location == null) {
  79.                         locationManager.requestLocationUpdates(
  80.                                 LocationManager.GPS_PROVIDER,
  81.                                 MIN_TIME_BW_UPDATES,
  82.                                 MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
  83.  
  84.                         if(locationManager != null) {
  85.                             location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  86.  
  87.                             if(location != null) {
  88.                                 latitude = location.getLatitude();
  89.                                 longitude = location.getLongitude();
  90.                             }
  91.                         }
  92.                     }
  93.                 }
  94.             }
  95.  
  96.         } catch (Exception e) {
  97.             e.printStackTrace();
  98.         }
  99.  
  100.         return location;
  101.     }
  102.  
  103.  
  104.     public void stopUsingGPS() {
  105.         if(locationManager != null) {
  106.             locationManager.removeUpdates(GPSTracker.this);
  107.         }
  108.     }
  109.  
  110.     public double getLatitude() {
  111.         if(location != null) {
  112.             latitude = location.getLatitude();
  113.         }
  114.         return latitude;
  115.     }
  116.  
  117.     public double getLongitude() {
  118.         if(location != null) {
  119.             longitude = location.getLongitude();
  120.         }
  121.  
  122.         return longitude;
  123.     }
  124.  
  125.     public boolean canGetLocation() {
  126.         return this.canGetLocation;
  127.     }
  128.  
  129.     public void showSettingsAlert() {
  130.         AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
  131.  
  132.         alertDialog.setTitle("Ustawienia GPS");
  133.  
  134.         alertDialog.setMessage("GPS jest wyłączony. Przejść do ustawień lokalizacji?");
  135.  
  136.         alertDialog.setPositiveButton("Ustawienia", new DialogInterface.OnClickListener() {
  137.  
  138.             @Override
  139.             public void onClick(DialogInterface dialog, int which) {
  140.                 Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  141.                 context.startActivity(intent);
  142.             }
  143.         });
  144.  
  145.         alertDialog.setNegativeButton("Anuluj", new DialogInterface.OnClickListener() {
  146.  
  147.             @Override
  148.             public void onClick(DialogInterface dialog, int which) {
  149.                 dialog.cancel();
  150.             }
  151.         });
  152.  
  153.         alertDialog.show();
  154.     }
  155.  
  156.  
  157.  
  158.         @Override
  159.         public void onLocationChanged(Location location) {
  160.  
  161.         }
  162.  
  163.         private void updateLocation(Location location) {
  164.  
  165.  
  166.         }
  167.  
  168.         @Override
  169.         public void onProviderDisabled(String arg0) {
  170.             // TODO Auto-generated method stub
  171.  
  172.         }
  173.  
  174.         @Override
  175.         public void onProviderEnabled(String arg0) {
  176.             // TODO Auto-generated method stub
  177.  
  178.         }
  179.  
  180.         @Override
  181.         public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
  182.             // TODO Auto-generated method stub
  183.  
  184.         }
  185.  
  186.     @Override
  187.     public IBinder onBind(Intent intent) {
  188.         // TODO Auto-generated method stub
  189.         return null;
  190.     }
  191.  
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement