2016-07-28 138 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; 
    } 
} 
相关问题