【问题标题】:Fetch continuous location changes with less battery drainage以更少的电池消耗获取连续的位置变化
【发布时间】:2018-10-27 08:01:56
【问题描述】:

我想连续获取用户位置并在我的数据库中更新它。 我正在使用 FusedLocationApi 来获取连续的位置更改。 要获得位置,用户必须打开 GPS 并且必须有互联网连接。 长时间保持 GPS 开启和持续使用 INTERNET 是电池耗尽的罪魁祸首。 所以我想知道应该怎么做才能使用最小的电池电量并获取连续的位置变化。

这就是我获取位置的方式,

public class LocationActivity extends Activity implements
    LocationListener,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {

private static final String TAG = "LocationActivity";
private static final long INTERVAL = 1000 * 10;
private static final long FASTEST_INTERVAL = 1000 * 5;
Button btnFusedLocation;
TextView tvLocation;
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
Location mCurrentLocation;
String mLastUpdateTime;

protected void createLocationRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(INTERVAL);
    mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "onCreate ...............................");
    //show error dialog if GoolglePlayServices not available
    if (!isGooglePlayServicesAvailable()) {
        finish();
    }
    createLocationRequest();
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    mGoogleApiClient.connect();  
    setContentView(R.layout.activity_main);
    tvLocation = (TextView) findViewById(R.id.tvLocation);

    btnFusedLocation = (Button) findViewById(R.id.btnShowLocation);
    btnFusedLocation.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            updateUI();
        }
    });

}

@Override
public void onStart() {
    super.onStart();
    if (mGoogleApiClient.isConnected()) {
        startLocationUpdates();
        Log.d(TAG, "Location update resumed .....................");
    }
}

@Override
public void onStop() {
    super.onStop();
    Log.d(TAG, "onStop fired ..............");
    mGoogleApiClient.disconnect();
    Log.d(TAG, "isConnected ...............: " + mGoogleApiClient.isConnected());
}

private boolean isGooglePlayServicesAvailable() {
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (ConnectionResult.SUCCESS == status) {
        return true;
    } else {
        GooglePlayServicesUtil.getErrorDialog(status, this, 0).show();
        return false;
    }
}

@Override
public void onConnected(Bundle bundle) {
    Log.d(TAG, "onConnected - isConnected ...............: " + mGoogleApiClient.isConnected());
    startLocationUpdates();
}

protected void startLocationUpdates() {
    PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient, mLocationRequest, this);
    Log.d(TAG, "Location update started ..............: ");
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.d(TAG, "Connection failed: " + connectionResult.toString());
}

@Override
public void onLocationChanged(Location location) {
    Log.d(TAG, "Firing onLocationChanged..............................................");
    mCurrentLocation = location;
    mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
    updateUI();
}

private void updateUI() {
    Log.d(TAG, "UI update initiated .............");
    if (null != mCurrentLocation) {
        String lat = String.valueOf(mCurrentLocation.getLatitude());
        String lng = String.valueOf(mCurrentLocation.getLongitude());
        tvLocation.setText("At Time: " + mLastUpdateTime + "\n" +
                "Latitude: " + lat + "\n" +
                "Longitude: " + lng + "\n" +
                "Accuracy: " + mCurrentLocation.getAccuracy() + "\n" +
                "Provider: " + mCurrentLocation.getProvider());
    } else {
        Log.d(TAG, "location is null ...............");
    }
}

@Override
protected void onPause() {
    super.onPause();
    stopLocationUpdates();
}

protected void stopLocationUpdates() {
    LocationServices.FusedLocationApi.removeLocationUpdates(
            mGoogleApiClient, this);
    Log.d(TAG, "Location update stopped .......................");
}

@Override
public void onResume() {
    super.onResume();
    if (mGoogleApiClient.isConnected()) {
        startLocationUpdates();
        Log.d(TAG, "Location update resumed .....................");
    }
}
}

【问题讨论】:

  • 我很确定你可以让 api 运行你的 onLocationChange 方法,但我不确定。
  • 一切正常。唯一的事情是我想要更少的电池消耗。
  • 是的,我认为这应该降低它,但如果你过一段时间没有更新的话,你可能必须有一个更新的东西。

标签: android android-gps android-fusedlocation batterylevel


【解决方案1】:

https://developer.android.com/guide/topics/location/strategies

Google 建议创建一个能够实现最佳性能的模型。解释链接的页面...

  • 选择合适的时间开始监听来自所需位置提供商的更新。
  • 通过过滤掉新的但不太准确的修复来维护“当前最佳估计”位置。
  • 暂时停止监听位置更新。
  • 利用最后的最佳位置估计。

连续的位置更改是解决问题的一种非常通用的方法,您需要针对非常具体的用例定义问题和解决方案。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-07-24
  • 2012-11-30
  • 2017-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
相关资源
最近更新 更多