【问题标题】:How can I reduce power usage in a location-based Android application?如何减少基于位置的 Android 应用程序的耗电量?
【发布时间】:2010-04-17 08:26:29
【问题描述】:

如何减少应用程序的耗电量?我可以使用什么代码来实现它?

【问题讨论】:

    标签: android


    【解决方案1】:

    在尝试获取位置信息时,有几种不同的方法可以降低功耗。

    1. 使用last known location 而不是尝试确定当前位置。

      // Get a Location Manager
      LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
      
      // Try to get the last GPS based location
      Location l = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
      
      // Fall back to cell tower based location if no prior GPS location
      if (l == null) {
          l = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
      }
      
    2. 使用较便宜的位置提供商。您可以直接选择LocationManager.NETWORK_PROVIDER 或指定您关心的criteria,让Android 告诉您使用哪个位置提供程序。

      // Select the criteria you care about
      Criteria c = new Criteria();
      c.setAccuracy(Criteria.ACCURACY_COARSE);
      c.setPowerRequirement(Criteria.POWER_LOW);
      
      // Let the system tell you what provider you should use for your criteria
      LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
      String p = lm.getBestProvider(c, true);
      
      // Call other Location Manager functions using the above provider...
      

    【讨论】:

    • 非常感谢我实施了
    • 在我的应用程序中电池使用量在 50% 它适合另一种电池使用和内存使用方式
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多