upsidedown

tracker

Feb 21st, 2014
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1. package com.example.adnan_test;
  2.  
  3. import android.app.Service;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.location.Location;
  7. import android.location.LocationListener;
  8. import android.location.LocationManager;
  9. import android.os.Bundle;
  10. import android.os.IBinder;
  11. import android.util.Log;
  12.  
  13. public class Tracker extends Service implements LocationListener {
  14. private final Context c;
  15.    
  16.     boolean isgps=false;
  17.     boolean isnet=false;
  18.     boolean cangetloc=false;
  19.     Location loc;
  20.     double lat,lon;
  21.    
  22.     private static final long mindist=10;
  23.     private static final long mintimt=1000*60*1;   
  24.    
  25.     protected LocationManager lm;
  26.    
  27.     public Tracker(Context x)
  28.     {
  29.         c=x;
  30.         getloc();
  31.     }
  32.  
  33.  
  34. public Location getloc()
  35. {
  36.     try
  37.     {
  38.         lm=(LocationManager)c.getSystemService(LOCATION_SERVICE);
  39.        
  40.         isgps=lm.isProviderEnabled(lm.GPS_PROVIDER);
  41.        
  42.         isnet=lm.isProviderEnabled(lm.NETWORK_PROVIDER);
  43.        
  44.         if((!isgps)&&(!isnet))
  45.         {
  46.            
  47.         }
  48.         else
  49.         {
  50.             this.cangetloc=true;
  51.            
  52.             if(isnet)
  53.             {
  54.                 lm.requestLocationUpdates(lm.NETWORK_PROVIDER,mintimt, mindist, this);
  55.                 Log.d("Network", "Network");
  56.                 if (lm != null) {
  57.                     loc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  58.                     if (loc != null) {
  59.                         lat = loc.getLatitude();
  60.                         lon = loc.getLongitude();
  61.                     }
  62.                 }
  63.             }
  64.            
  65.             if (isgps) {
  66.                 if (loc == null) {
  67.                     lm.requestLocationUpdates(lm.GPS_PROVIDER,mintimt,mindist, this);
  68.                     Log.d("GPS Enabled", "GPS Enabled");
  69.                     if (lm != null) {
  70.                         loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  71.                         if (loc != null) {
  72.                             lat = loc.getLatitude();
  73.                             lon = loc.getLongitude();
  74.                         }
  75.                     }
  76.                 }
  77.             }
  78.         }
  79.  
  80.     } catch (Exception e) {
  81.         e.printStackTrace();
  82.     }
  83.  
  84.     return loc;
  85. }
  86.  
  87. public void onLocationChanged(Location location) {
  88. }
  89.  
  90. @Override
  91. public void onProviderDisabled(String provider) {
  92. }
  93.  
  94. @Override
  95. public void onProviderEnabled(String provider) {
  96. }
  97.  
  98. @Override
  99. public void onStatusChanged(String provider, int status, Bundle extras) {
  100. }
  101.  
  102. @Override
  103. public IBinder onBind(Intent arg0) {
  104.     return null;
  105. }
  106.  
  107. public double getlat(){
  108.     if(loc != null){
  109.         lat = loc.getLatitude();
  110.     }
  111.      
  112.     // return latitude
  113.     return lat;
  114. }
  115.  
  116. /**
  117.  * Function to get longitude
  118.  * */
  119. public double getlon()
  120. {
  121.     if(loc != null)
  122.     {
  123.         lon = loc.getLongitude();
  124.     }
  125.      
  126.     // return longitude
  127.     return lon;
  128. }
  129.  
  130. public boolean canGetLocation()
  131. {
  132.     return this.cangetloc;
  133. }
  134.  
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment