如何降低应用程序的功耗?我可以使用什么代码来实现它?
最佳答案
有几种不同的方法可以降低尝试获取位置信息时使用的电量。
使用 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); }
使用价格较低的位置提供商。您可以选择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/