Android 获取连续的位置更改,减少电池放电

Android 获取连续的位置更改,减少电池放电,android,android-gps,android-fusedlocation,batterylevel,Android,Android Gps,Android Fusedlocation,Batterylevel,我想连续获取用户位置并在数据库中更新它。 我正在使用FusedLocationApi来获得连续的位置更改。 要获得位置,用户必须打开GPS和互联网连接。 长时间保持GPS转动和持续使用互联网是导致电池放电的主要原因。 所以我想知道应该做些什么来使用最小的电池电量和获取连续的位置变化 这就是我获取位置的方式 public class LocationActivity extends Activity implements LocationListener, GoogleApiCli

我想连续获取用户位置并在数据库中更新它。 我正在使用FusedLocationApi来获得连续的位置更改。 要获得位置,用户必须打开GPS和互联网连接。 长时间保持GPS转动和持续使用互联网是导致电池放电的主要原因。 所以我想知道应该做些什么来使用最小的电池电量和获取连续的位置变化

这就是我获取位置的方式

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 .....................");
    }
}
}
公共类LocationActivity扩展活动实现
LocationListener,
GoogleAppClient.ConnectionCallbacks,
GoogleAppClient.OnConnectionFailedListener{
私有静态最终字符串TAG=“LocationActivity”;
专用静态最终长间隔=1000*10;
专用静态最终最长最快_间隔=1000*5;
按钮BTN使用位置;
文本视图位置;
位置请求mLocationRequest;
GoogleapClient MGoogleapClient;
位置mCurrentLocation;
字符串mLastUpdateTime;
受保护的void createLocationRequest(){
mlLocationRequest=新位置请求();
mlLocationRequest.setInterval(间隔);
mLocationRequest.SetFastTestInterval(最快间隔);
mLocationRequest.setPriority(位置请求.优先级高精度);
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
日志d(标签“onCreate”);
//如果GoolglePlayServices不可用,则显示错误对话框
如果(!isGooglePlayServicesAvailable()){
完成();
}
createLocationRequest();
mgoogleapclient=新的Googleapclient.Builder(此)
.addApi(LocationServices.API)
.addConnectionCallbacks(此)
.addOnConnectionFailedListener(此)
.build();
mGoogleApiClient.connect();
setContentView(R.layout.activity_main);
tvLocation=(TextView)findViewById(R.id.tvLocation);
btnFusedLocation=(按钮)findViewById(R.id.btnShowLocation);
btnFusedLocation.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图arg0){
updateUI();
}
});
}
@凌驾
public void onStart(){
super.onStart();
if(mgoogleapClient.isConnected()){
startLocationUpdates();
Log.d(标签“位置更新恢复…”);
}
}
@凌驾
公共void onStop(){
super.onStop();
日志d(标签“顶部点火……”);
mGoogleApiClient.disconnect();
Log.d(标记“isConnected…”:“+mgoogleapclient.isConnected());
}
私有布尔值isGooglePlayServicesAvailable(){
int status=GooglePlayServicesUtil.isGooglePlayServicesAvailable(此);
if(ConnectionResult.SUCCESS==状态){
返回true;
}否则{
GooglePlayServicesUtil.getErrorDialog(状态,this,0.show();
返回false;
}
}
@凌驾
未连接的公共空间(捆绑包){
Log.d(标记“onConnected-isConnected…”:“+mgoogleapclient.isConnected()”;
startLocationUpdates();
}
受保护的void startLocationUpdates(){
Pendingreult Pendingreult=LocationServices.FusedLocationApi.RequestLocationUpdate(
mgoogleapclient,mLocationRequest,this);
Log.d(标记“位置更新已开始…”);
}
@凌驾
公共空间连接暂停(int i){
}
@凌驾
公共无效onConnectionFailed(ConnectionResult ConnectionResult){
Log.d(标记“连接失败:”+connectionResult.toString());
}
@凌驾
已更改位置上的公共无效(位置){
Log.d(标签“仅点火位置已更改”);
mCurrentLocation=位置;
mLastUpdateTime=DateFormat.getTimeInstance().format(新日期());
updateUI();
}
私有void updateUI(){
Log.d(标记“UI更新已启动……”);
if(null!=mCurrentLocation){
String lat=String.valueOf(mCurrentLocation.getLatitude());
String lng=String.valueOf(mCurrentLocation.getLongitude());
tvLocation.setText(“在时间:+mLastUpdateTime+”\n”+
纬度:“+lat+”\n+
经度:“+lng+”\n+
精度:“+mCurrentLocation.GetAccurance()+”\n”+
提供程序:“+mCurrentLocation.getProvider());
}否则{
Log.d(标记“位置为空……”);
}
}
@凌驾
受保护的void onPause(){
super.onPause();
stopLocationUpdates();
}
受保护的void stopLocationUpdates(){
LocationServices.FusedLocationApi.RemovelocationUpdate(
mGoogleApiClient,本);
Log.d(标记“位置更新已停止…”);
}
@凌驾
恢复时公开作废(){
super.onResume();
if(mgoogleapClient.isConnected()){
startLocationUpdates();
Log.d(标签“位置更新恢复…”);
}
}
}

谷歌建议创建一个能够获得最佳性能的模型。要对链接页面进行解释

  • 选择正确的时间开始侦听来自所需位置提供程序的更新
  • 通过过滤掉新的但不太准确的修复,保持对位置的“当前最佳估计”
  • 暂时停止侦听位置更新
  • 利用最后的最佳位置估计

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

我非常确定您可以让api运行您的方法来进行onLocationChange或其他操作,但我不确定。一切都正常。唯一的问题是我想要更少的电池消耗。是的,我认为这应该会降低它,如果你过了一段时间还没有更新的话,你可能需要有一个能得到更新的东西