2013-02-28 93 views
0

我開發了一款應用程序,每隔30秒使用當前位置。該應用程序已完成。它完美地運行在ICS(Android的4),豆形軟糖(是Android 4.1)設備,但是當我每隔2/3秒運行在在Android 2.3或Android 2.2,應用GPS更新該應用後和不斷更新。儘管我的代碼完成了30秒,並且它在Android 4.4.1中完美運行(30秒後),但不適用於Android 2.3或2.2。在Android 2.2和Android 2.3中不斷更新GPS位置

我共享代碼....

public class GPSManager 
{ 
    //private static final int gpsMinTime = 60000; 
    //private static final int gpsMinDistance = 0; 

    private long gpsMinTime = 0; 
    private float gpsMinDistance = 0; 

    private static LocationManager locationManager = null; 
    private static LocationListener locationListener = null; 
    private static GPSCallback gpsCallback = null; 

    public GPSManager(long gpsMinTime,float gpsMinDistance) 
    { 
     this.gpsMinTime = gpsMinTime; 
     this.gpsMinDistance = gpsMinDistance; 

     GPSManager.locationListener = new LocationListener() 
     { 
      @Override 
      public void onLocationChanged(final Location location) 
      { 
       if (GPSManager.gpsCallback != null) 
       { 
        GPSManager.gpsCallback.onGPSUpdate(location); 
       } 
      } 

      @Override 
      public void onProviderDisabled(final String provider) 
      { 

      } 

      @Override 
      public void onProviderEnabled(final String provider) 
      { 

      } 

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

      } 
     }; 
    } 

    public GPSCallback getGPSCallback() 
    { 
     return GPSManager.gpsCallback; 
    } 

    public void setGPSCallback(final GPSCallback gpsCallback) 
    { 
     GPSManager.gpsCallback = gpsCallback; 
    } 

    public void startListening(final Context context) 
    { 
     if (GPSManager.locationManager == null) 
     { 
      GPSManager.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 
     } 

     final Criteria criteria = new Criteria(); 

     criteria.setAccuracy(Criteria.ACCURACY_MEDIUM); // accuracy_midium 
     criteria.setSpeedRequired(true); 
     criteria.setAltitudeRequired(false); 
     criteria.setBearingRequired(true); 
     criteria.setCostAllowed(true); 
     criteria.setPowerRequirement(Criteria.POWER_MEDIUM); 

     final String bestProvider = GPSManager.locationManager.getBestProvider(criteria, true); 

     if (bestProvider != null && bestProvider.length() > 0) 
     { 
      GPSManager.locationManager.requestLocationUpdates(bestProvider, this.gpsMinTime, 
        this.gpsMinDistance, GPSManager.locationListener); 

     } 
     else 
     { 
      final List<String> providers = GPSManager.locationManager.getProviders(true); 

      for (final String provider : providers) 
      { 
       GPSManager.locationManager.requestLocationUpdates(provider, this.gpsMinTime, 
         this.gpsMinDistance, GPSManager.locationListener); 
      } 
     } 
    } 

    public void stopListening() 
    { 
     try 
     { 
      if (GPSManager.locationManager != null && GPSManager.locationListener != null) 
      { 
       GPSManager.locationManager.removeUpdates(GPSManager.locationListener); 
      } 

      GPSManager.locationManager = null; 
     } 
     catch (final Exception ex) 
     { 

     } 
    } 


} 

我的位置更新的代碼是在這裏:

private static final long GPS_MINTIME = 30000; 
private static final float GPS_MINDISTANCE = 100; 
gpsManager = new GPSManager(GPS_MINTIME,GPS_MINDISTANCE); 
       gpsManager.startListening(getApplicationContext()); 
       gpsManager.setGPSCallback(this); 

任何想法,爲什麼發生這種情況。謝謝。

+0

我發現很多我的手機上是相同的。 2.3版時,minUpdateTime似乎被忽略了,一旦操作系統升級到ICS,它就會兌現合同。 – NickT 2013-02-28 19:12:09

+0

你確定你設置的最短時間爲'30Secs',我看到你在當前代碼中設置爲'0' – VenomVendor 2013-02-28 19:17:38

+0

這很奇怪!運行OS 2.3/2.2的應用程序每2/3秒後閃爍/閃爍以更新GPS當前位置。驚訝! – arefin 2013-02-28 19:19:43

回答

0

變化GPS_MINTIME大於6000
它會很好地工作。

+0

我的GPS_MINTIME是30秒(30000毫秒),你測試過@VenomVendor嗎? – arefin 2013-02-28 19:17:52

+0

請重新檢查您的代碼,它是'0秒' – VenomVendor 2013-02-28 19:21:58

+0

@VenomVendor - 正確讀取它,它是30秒 – NickT 2013-02-28 19:31:39

0

請檢查SO https://stackoverflow.com/a/4245527/1438915

在API < 4.0分鐘的時間

是不是強制性考慮 考慮它可能是不準確的,有時這個答案。 的作品像提示

UPDATE

從文檔INFO我想你應該閱讀。

+0

應用程序使用30000ms(30秒),最小距離是100,所以現在有什麼想法? – arefin 2013-02-28 19:29:07

+0

@ user748178對不起,我的錯誤。 – Siddhesh 2013-02-28 19:32:54

+0

感謝您的回覆。如果您找到任何解決方案,請在此分享。 – arefin 2013-02-28 19:35:18

0

繼我的意見,這是一個衆所周知的(但不是廣泛報道的bug /功能)。見 Google Groups Post 我沒加我支持這一提議,但該線程現在已經關閉,因爲它是固定的4.1

+0

這意味着,我必須用這個bug上傳應用程序?任何建議? – arefin 2013-02-28 19:52:35

+0

我最終將自己的調度程序放在綁定服務中。我將MY min更新時間作爲參數傳遞給運行一個由定時器控制的小型狀態機的服務。 – NickT 2013-02-28 19:56:02

+0

我還計劃在自己的調度程序/定時器中控制gps更新。謝謝@NickT – arefin 2013-02-28 20:01:02