2016-07-28 85 views
0

我開發了一個android應用程序,它在一定時間後檢查位置更新並將位置數據發送到服務器。我測試了兩種方法;一個使用Google位置apI,另一個使用Android服務。但是這兩款應用都消耗了太多電量。我需要優化電池的使用,以便用戶可以根據需要使用應用程序。在android中的位置服務的應用程序

有幾個應用程序始終跟蹤我們的位置,永遠不會妨礙電池;例如endomondo,Facebook等。特別我想提到endomondo,因爲它在工作時確實可以正常工作。

任何人都可以請建議我最好的方式來實現android應用程序的位置感知?

如何讓我的應用程序像endomondo或類似的位置感知應用程序?

請給我建議更好的方法來做到這一點。

在此先感謝。

回答

0

你試過用支票來減少GPS定位跟蹤,如果LocationManagar具有位置好夠你(也許其他應用程序跟蹤?)

例:?

private static Location bestLastKnownLocation(float minAccuracy, long maxAge, LocationManager mLocationManager) { 

    Location bestResult = null; 
    float bestAccuracy = Float.MAX_VALUE; 
    long bestAge = Long.MIN_VALUE; 

    List<String> matchingProviders = mLocationManager.getAllProviders(); 

    for (String provider : matchingProviders) { 

     Location location = mLocationManager.getLastKnownLocation(provider); 

     if (location != null) { 

      float accuracy = location.getAccuracy(); 
      long time = location.getTime(); 

      if (accuracy < bestAccuracy) { 

       bestResult = location; 
       bestAccuracy = accuracy; 
       bestAge = time; 

      } 
     } 
    } 

    // Return best reading or null 
    if (bestAccuracy > minAccuracy || (System.currentTimeMillis() - bestAge) > maxAge) { 
     return null; 
    } else { 
     return bestResult; 
    } 
} 
相關問題