2016-08-15 3 views
7

잘못된 시간을 반환 그것은 나에게 좀 잘 경도와 위도하지만를 제공합니다 location.getTime() 나에게 잘못된 시간을 준다, 그것은 300000 mil 정도의 시간을 되 돌린다. 그것은 1471243684497과 같아야합니다.안드로이드 onLocationChanged location.getTime은() 내가 <code>android.location.LocationListener</code> 및 <code>onLocationChanged(Location location)</code>을 구현 한 삼성 갤럭시 탭 A.이 호출되는 한

나는 무엇을 해야할지 잘 모릅니다. 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.isProviderEnabled(LocationManager.GPS_PROVIDER)이 경우 확인하실 수 있습니다) (을 locationManager은 시대 이후 장치의 가동 시간이 아닌 시간에 위치를 반환) - 가능하다면, 당신이 버그에 대한 링크를 제공하시기 바랍니다 수 있습니까? 이 질문을 제외하고 아무것도 찾을 수 없습니다. 많은 감사합니다! –

+1

@AlephAleph, 나는 "퓨즈 위치"가 아닌 내 자신의 간단한 위치 수신기를 구현하게되었습니다. PARTIAL_WAKE_LOCK 유형의 웨이크 잠금 장치와 섞어서,'Locationmanager'를 등록하여 네트워크 공급자와 gps 공급자를 청취했습니다. 이제는 잘 작동합니다. –

답변

0

NMEA 위치 수신기를 사용할 수 있으며 그 시간 만 표시됩니다. 기기의 시간대 나 날짜와 시간을 변경하더라도 항상 사실이며 UTC 시간입니다. 이는 현재 실제 날짜와 시간을 제공합니다.

Limitaions : GPS 또는 위치 -if

는 NMEA을받지 않습니다, 꺼져 있습니다. (위치가 켜져 있는지 여부를 확인할 수 있음)

- 위치가 켜져 있지만 사용자가 설정을 통해서만 네트워크의 정확도가 변경되면 NMEA를 얻을 수 없습니다. 는