Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 具有处理程序时间延迟的Android位置后台服务_Java_Android_Location - Fatal编程技术网

Java 具有处理程序时间延迟的Android位置后台服务

Java 具有处理程序时间延迟的Android位置后台服务,java,android,location,Java,Android,Location,我可以在定位服务中使用handler以提高电池效率吗? 我的应用程序有一个在后台工作的服务,并在更新位置(GET_location_time)内设置各种延迟时间。 当我检索到位置时,我使用循环处理程序启动和停止FusedLocationApi—将其发送给Firebase @Override public void run() { handler.postDelayed(this, updateTime); startLocationUpdates(); if (myLo

我可以在定位服务中使用handler以提高电池效率吗? 我的应用程序有一个在后台工作的服务,并在更新位置(GET_location_time)内设置各种延迟时间。 当我检索到位置时,我使用循环处理程序启动和停止FusedLocationApi—将其发送给Firebase

 @Override
public void run() {
    handler.postDelayed(this, updateTime);
    startLocationUpdates();
    if (myLocation != null) {
        mFirebaseDatabaseHelper.updateUserLocation(myLocation);
    }
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            stopLocationUpdates();

        }
    }, GET_LOCATION_TIME);
}

private void startLocationUpdates() {
    //handle unexpected permission absence
    if (mGoogleApiClient.isConnected()) {
        if (checkLocationPermission()) {
            LocationServices.FusedLocationApi.requestLocationUpdates(
                    mGoogleApiClient, createLocationRequest(), this);
        }
    } else {
        mGoogleApiClient.connect();
    }
}

private LocationRequest createLocationRequest() {
    LocationRequest mLocationRequest = new LocationRequest();

    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    mLocationRequest.setInterval(1000); //1 secs
    mLocationRequest.setFastestInterval(100); //1 secs
    mLocationRequest.setSmallestDisplacement(1f); //1 m

    return mLocationRequest;

}

private void stopLocationUpdates() {
    if (mGoogleApiClient.isConnected()) {
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
    }
}