Я разрабатываю приложение, которое с самого начала использует службу определения местоположения. В моем телефоне (Sony Z3 Compact) есть список приложений, которые использовали службу определения местоположения в меню настроек местоположения. В нем есть каждое приложение с сообщением об использовании батареи, например, низкий уровень использования батареи, высокий уровень использования батареи и т. д. Мое приложение указано как «Высокое использование батареи». Другие приложения, которые используют данные о местоположении, такие как Tinder, имеют это как «низкое использование батареи».
Я хотел бы знать, что вызывает это и как лучше всего экономить заряд батареи. Я вижу, что у каждого провайдера местоположения есть данные об «использовании батареи». Означает ли это, что если я использую только провайдера сети, а не провайдера GPS, я получаю отметку «Низкое использование батареи»?
Или это зависит от запроса обновлений местоположения? Поскольку мне нужно местоположение только один раз, однако я могу проверить, переместился ли пользователь, для этого мне нужно включить обновления местоположения.
Любые идеи по этому поводу?
Вот сервис, которым я пользуюсь:
public class LocationTracker2 extends Service implements LocationListener {
protected Context context;
protected LocationManager locationManager;
protected OnLocationChanged event;
protected Location currentLocation;
public interface OnLocationChanged {
public void onLocationChanged(double latitude, double longitude);
}
public LocationTracker2(Context context, OnLocationChanged event) {
this.context = context;
this.event = event;
locationManager = (LocationManager)context.getSystemService(LOCATION_SERVICE);
}
public boolean canGetLocation() {
// getting GPS status
boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
return (isGPSEnabled || isNetworkEnabled);
}
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
// Setting Dialog Title
alertDialog.setTitle("GPS is settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
public Location getCurrentLocation() {
if(currentLocation != null) return currentLocation;
List<String> providers = locationManager.getProviders(true);
Location bestLocation = null;
for (String provider : providers) {
Location l = locationManager.getLastKnownLocation(provider);
if (l == null) {
continue;
}
if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
// Found best last known location: %s", l);
bestLocation = l;
}
}
if(bestLocation != null) {
currentLocation = bestLocation;
this.event.onLocationChanged(currentLocation.getLatitude(), currentLocation.getLongitude());
}
return currentLocation;
}
public void startLocationUpdates() {
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, false);
locationManager.requestLocationUpdates(bestProvider, 60000, 10, this);
}
public void stopLocationUpdates() {
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) {
this.currentLocation = location;
this.event.onLocationChanged(this.currentLocation.getLatitude(), this.currentLocation.getLongitude());
}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}