我怎样才能减少用电量在基于位置的Android应用程序? [英] How can I reduce power usage in a location-based Android application?
问题描述
我怎样才能减少我的应用程序的电源使用情况?什么code,我可以用它来实现这一点?
How can I reduce the power usage in my application? What code can I use to implement this?
推荐答案
有几个不同的方式试图获得位置信息时减少所使用的电源。
There are a couple different ways to reduce the power used when trying to get location information.
-
使用的<一个href="http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation%28java.lang.String%29">last已知地点而不是试图确定当前位置。
Use the last known location instead of trying to determine the current 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);
}
使用更便宜的位置提供。你可以挑选<一个href="http://developer.android.com/reference/android/location/LocationManager.html#NETWORK_PROVIDER">LocationManager.NETWORK_PROVIDER直接或指定你关心的标准,让Android的告诉你使用哪个位置提供。
Use a less expensive location provider. You can pick LocationManager.NETWORK_PROVIDER directly or specify the criteria you care about and let Android tell you which location provider to use.
// 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应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!