将jobScheduler与融合位置服务一起使用

时间:2018-08-06 11:35:53

标签: android android-jobscheduler fusedlocationproviderapi

我使用JobScheduler使用JobService在后​​台将用户的位置定期发送到服务器。我对使用JobScheduler设置的时间表和requestLocationUpdates设置的时间间隔感到困惑。

假设我想每30秒发送一次位置信息。每次计划程序使用getLastLocation()运行时,我是否应该仅使用setMinimumLatency(30000)获得最后的已知位置?还是我应该一次运行调度程序,然后让FusedLocationApi使用mLocationRequest.setInterval(30000);每30秒发送一次位置?

如果两种方法都可以,那么最好在后台服务中发送位置信息?

目前,我在JobScheduler和FusedLocationApi上都有间隔,但是我不能决定下一步该怎么做:

这是我在MainActivity中的调度程序:

    ComponentName serviceComponent = new ComponentName(this, LocationMonitoringService.class);
    JobInfo.Builder builder = new JobInfo.Builder(0, serviceComponent);
    builder.setMinimumLatency(30 * 1000); // wait at least
    builder.setOverrideDeadline(30 * 1000); // maximum delay
    builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
    JobScheduler jobScheduler = this.getSystemService(JobScheduler.class);
    jobScheduler.schedule(builder.build());

这是我的LocationMonitoringService:

public class LocationMonitoringService extends JobService implements
        GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
        LocationListener {

    private static final String TAG = LocationMonitoringService.class.getSimpleName();
    boolean isWorking = false;
    boolean jobCancelled = false;
    GoogleApiClient mLocationClient;
    LocationRequest mLocationRequest = new LocationRequest();

    @Override
    public boolean onStartJob(JobParameters jobParameters) {
        mLocationClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        mLocationRequest.setInterval(10000);
        mLocationRequest.setFastestInterval(5000);
        int priority = LocationRequest.PRIORITY_HIGH_ACCURACY;
        mLocationRequest.setPriority(priority);
        mLocationClient.connect();
        return true;
    }

    @Override
    public boolean onStopJob(JobParameters jobParameters) {
        Log.d(TAG, "Job cancelled before being completed.");
        jobCancelled = true;
        boolean needsReschedule = isWorking;
        jobFinished(jobParameters, needsReschedule);
        return needsReschedule;
    }

    /*
     * LOCATION CALLBACKS
     */
    @Override
    public void onConnected(Bundle dataBundle) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        LocationServices.FusedLocationApi.requestLocationUpdates(mLocationClient, mLocationRequest, this);
        Log.d(TAG, "Connected to Google API");
    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.d(TAG, "Connection suspended");
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.d(TAG, "Location changed");

        if (location != null) {
            Log.d(TAG, "== location != null");
            //Send result to Server
        }
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.d(TAG, "Failed to connect to Google API");

    }
}

0 个答案:

没有答案