android - 如何降低基于位置的 Android 应用程序的功耗?

标签 android

如何降低应用程序的功耗?我可以使用什么代码来实现它?

最佳答案

有几种不同的方法可以降低尝试​​获取位置信息时使用的电量。

  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...
    

关于android - 如何降低基于位置的 Android 应用程序的功耗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2657766/

相关文章:

android - 如何在多个 Activity 中显示相同的对话框?

android - 如何使用 Realm 将预加载数据显示到 RecyclerView

Android: Gingerbread 上的 YouTubePlayerFragment 出现 NoClassDefFoundError

java - 在 android 的 editText 中使用 R.String

java - Volley jsonArray 传递

android - GmsClient "calling connect() while still connected"暂停时

android - 排除特定版本的 jar 或模块文件?

android - Flurry Analytics 在 Android 模拟器中为 "Run"时崩溃,但在模拟器中为 "Debugged"时不会崩溃

android - 如何从 Sqlite 数据库获取选定的 Spinner 值 ID

android - 如何使用 perl 存储和显示 ISO-8859-1 和 UTF8 字符