【发布时间】:2023-03-30 17:59:01
【问题描述】:
我正在开发一个从一开始就使用定位服务的应用程序。我的手机(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;
}
}
【问题讨论】:
标签: android performance android-location