2016-08-15 20 views
7

मैं एक सैमसंग गैलेक्सी टैब ए मैं android.location.LocationListener और onLocationChanged(Location location) को लागू किया है कहा जाता हो जाता है देता है और यह मेरे थोड़े सही देशांतर और अक्षांश लेकिन देता है,एंड्रॉयड onLocationChanged location.getTime() गलत समय

समस्या है: location.getTime() मुझे गलत समय देता है, यह 300000 मिलियन या उससे भी कम समय देता है। यह 1471243684497 जैसा कुछ होना चाहिए।

मुझे नहीं पता कि क्या करना है।

GPSTracker वर्ग:

import java.io.IOException; 
import java.util.List; 
import java.util.Locale; 

import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Address; 
import android.location.Geocoder; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.util.Log; 

public class GPSTracker extends Service implements LocationListener 
{ 
    //flag for GPS Status 
    boolean isGPSEnabled = false; 

    //flag for network status 
    //boolean isNetworkEnabled = false; 

    boolean canGetLocation = false; 

    Location location; 
    double latitude; 
    double longitude; 

    //The minimum distance to change updates in metters 
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; //10 metters 

    //The minimum time beetwen updates in milliseconds 
    private static final long MIN_TIME_BW_UPDATES = 1000 * 10; // 1 minute 

    //Declaring a Location Manager 
    protected LocationManager locationManager; 

    /*public GPSTracker(Context context) 
    { 
     this.mContext = context; 
     getLocation(); 
    }*/ 

    public Location getLocation(Context ctx) 
    { 
     try 
     { 
      locationManager = (LocationManager) ctx.getSystemService(LOCATION_SERVICE); 

      //getting GPS status 
      isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 

      //getting network status 
      //isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

      if (!isGPSEnabled /*&& !isNetworkEnabled*/) 
      { 
       // no network provider is enabled 
      } 
      else 
      { 
       this.canGetLocation = true; 

       //First get location from Network Provider 
       /* if (isNetworkEnabled) 
       { 
        locationManager.requestLocationUpdates(
          LocationManager.NETWORK_PROVIDER, 
          MIN_TIME_BW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 

        Log.d("Network", "Network"); 

        if (locationManager != null) 
        { 
         location = 
           locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
         updateGPSCoordinates(); 
        } 
       }*/ 

       //if GPS Enabled get lat/long using GPS Services 
       if (isGPSEnabled) 
       { 
        if (location == null) 
        { 
         locationManager.requestLocationUpdates(
           LocationManager.GPS_PROVIDER, 
           MIN_TIME_BW_UPDATES, 
           MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 

         Log.d("GPS Enabled", "GPS Enabled"); 

         if (locationManager != null) 
         { 
          location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
          updateGPSCoordinates(); 
         } 
        } 
       } 
      } 
     } 
     catch (Exception e) 
     { 
      //e.printStackTrace(); 
      Log.e("Error : Location", "Impossible to connect to LocationManager", e); 
     } 

     return location; 
    } 

    public void updateGPSCoordinates() 
    { 
     if (location != null) 
     { 
      latitude = location.getLatitude(); 
      longitude = location.getLongitude(); 
     } 
    } 

    /** 
    * Stop using GPS listener 
    * Calling this function will stop using GPS in your app 
    */ 

    public void stopUsingGPS() 
    { 
     if (locationManager != null) 
     { 
      locationManager.removeUpdates(GPSTracker.this); 
     } 
    } 

    /** 
    * Function to get latitude 
    */ 
    public double getLatitude() 
    { 
     if (location != null) 
     { 
      latitude = location.getLatitude(); 
     } 

     return latitude; 
    } 

    /** 
    * Function to get longitude 
    */ 
    public double getLongitude() 
    { 
     if (location != null) 
     { 
      longitude = location.getLongitude(); 
     } 

     return longitude; 
    } 

    /** 
    * Function to check GPS/wifi enabled 
    */ 
    public boolean canGetLocation() 
    { 
     if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)/*&& 
       !locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)*/){ 
      this.canGetLocation = false; 
     } else { 
      this.canGetLocation = true; 
     } 
     return this.canGetLocation; 
    } 

    /** 
    * Get list of address by latitude and longitude 
    * @return null or List<Address> 
    */ 
    public List<Address> getGeocoderAddress(Context context) 
    { 
     if (location != null) 
     { 
      Geocoder geocoder = new Geocoder(context, Locale.ENGLISH); 
      try 
      { 
       List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1); 
       return addresses; 
      } 
      catch (IOException e) 
      { 
       //e.printStackTrace(); 
       Log.e("Error : Geocoder", "Impossible to connect to Geocoder", e); 
      } 
     } 

     return null; 
    } 

    /** 
    * Try to get AddressLine 
    * @return null or addressLine 
    */ 
    public String getAddressLine(Context context) 
    { 
     List<Address> addresses = getGeocoderAddress(context); 
     if (addresses != null && addresses.size() > 0) 
     { 
      Address address = addresses.get(0); 
      String addressLine = address.getAddressLine(0); 

      return addressLine; 
     } 
     else 
     { 
      return null; 
     } 
    } 

    /** 
    * Try to get Locality 
    * @return null or locality 
    */ 
    public String getLocality(Context context) 
    { 
     List<Address> addresses = getGeocoderAddress(context); 
     if (addresses != null && addresses.size() > 0) 
     { 
      Address address = addresses.get(0); 
      String locality = address.getLocality(); 

      return locality; 
     } 
     else 
     { 
      return null; 
     } 
    } 

    /** 
    * Try to get Postal Code 
    * @return null or postalCode 
    */ 
    public String getPostalCode(Context context) 
    { 
     List<Address> addresses = getGeocoderAddress(context); 
     if (addresses != null && addresses.size() > 0) 
     { 
      Address address = addresses.get(0); 
      String postalCode = address.getPostalCode(); 

      return postalCode; 
     } 
     else 
     { 
      return null; 
     } 
    } 

    /** 
    * Try to get CountryName 
    * @return null or postalCode 
    */ 
    public String getCountryName(Context context) 
    { 
     List<Address> addresses = getGeocoderAddress(context); 
     if (addresses != null && addresses.size() > 0) 
     { 
      Address address = addresses.get(0); 
      String countryName = address.getCountryName(); 

      return countryName; 
     } 
     else 
     { 
      return null; 
     } 
    } 

    @Override 
    public void onLocationChanged(Location location) 
    { 
    } 

    @Override 
    public void onProviderDisabled(String provider) 
    { 
    } 

    @Override 
    public void onProviderEnabled(String provider) 
    { 
    } 


    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 

    } 

    @Override 
    public IBinder onBind(Intent arg0) { 
     return null; 
    } 
} 

और अपने ही वर्ग जो GPSTracker वर्ग लागू होता है:

public class NewMainService extends GPSTracker { 


    @Override 
    public void onLocationChanged(Location location) { 
     long time = location.getTime(); 
     Log.i("TAG", "onLocationChanged time: "+time); //prints wrong timestamp 
     super.onLocationChanged(location); 
    } 
} 

संपादित:

सब कुछ अन्य उपकरणों पर ठीक काम करता है।

मैंने इसे गुगल किया और यह कुछ उपकरणों में एक बग है। स्थान श्रोताओं कुछ समय बाद परिवर्तन प्राप्त करना बंद कर देते हैं।

वह समय मुझे डिवाइस के अपटाइम की तरह है, यह कुछ घंटे है, यह एक तारीख नहीं है।

+0

एक ही मुद्दा था (LocationManager बजाय अवधि के बाद डिवाइस के सक्रिय रहने की अवधि समय के साथ एक स्थान देता है) - यदि संभव हो तो, आप बग के बारे में लिंक दे कृपया कर सकते हैं? इस SO प्रश्न को छोड़कर कुछ भी नहीं मिला। बहुत धन्यवाद! –

+1

@AlephAleph, मैंने अपना खुद का सरल स्थान श्रोता लागू करने और "फ़्यूज्ड स्थान" नहीं लागू किया। और इसे PARTIAL_WAKE_LOCK प्रकार के लॉक लॉक के साथ मिश्रित किया, मैंने नेटवर्क प्रदाता और जीपीएस प्रदाता दोनों को सुनने के लिए 'Locationmanager' पंजीकृत किया। और अब यह ठीक काम कर रहा है। –

उत्तर

0

आप NMEA स्थान श्रोता का उपयोग कर सकते हैं और केवल उस समय से प्राप्त कर सकते हैं। यह हमेशा सत्य है और यूटीसी समय में है भले ही आप अपने डिवाइस के टाइमज़ोन या दिनांक और समय को बदल दें, यह आपको हमेशा वास्तविक वास्तविक तिथि और समय देगा।

Limitaions:

GPS या स्थान -यदि आपकी NMEA नहीं मिलेगा बंद है,। (आप जांच सकते हैं कि स्थान चालू है या नहीं)

-if स्थान चालू है लेकिन उपयोगकर्ता ने सेटिंग के माध्यम से केवल नेटवर्क पर अपनी सटीकता बदल दी है, आपको एनएमईए नहीं मिलेगा। (आप देख सकते हैं कि locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))

संबंधित मुद्दे