如何减少应用程序的功耗?我可以使用什么代码来实现这个?
有几种不同的方法可以减少尝试获取位置信息时所用的电量。
-
Use the 最后已知位置 http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation%28java.lang.String%29而不是尝试确定当前位置。
// 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 http://developer.android.com/reference/android/location/LocationManager.html#NETWORK_PROVIDER直接或指定criteria http://developer.android.com/reference/android/location/Criteria.html您关心的位置并让 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...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)