使用Job Scheduler API在作业服务中使用GoogleApiClient - 缺少回调

时间:2017-04-15 19:37:06

标签: java android scheduler google-api-client google-location-services

我正在尝试在应用关闭时以特定时间间隔获取位置,为此我尝试使用GoogleApiClient获取最后一个已知位置并使用JobService触发该位置。我在JobScheduler类上实现了ConnectionCallbacks。但问题是从不调用onConnected()回调。我认为在返回任何回调之前服务被销毁。那么实现这种逻辑的有效方法是什么。

我的职业服务班

public class TestJobService extends JobService
{
protected GoogleApiClient mGoogleApiClient;
protected Location mLastLocation;
protected JobParameters mJobParameters;
public TestJobService() {
    super();
}
private class GetLocation extends AsyncTask<Integer, Void, Integer> implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener {
    protected Integer doInBackground(Integer... jobID) {
        if (mGoogleApiClient == null) {
            mGoogleApiClient = new GoogleApiClient.Builder(getBaseContext())
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API)
                    .build();
        }

        return jobID[0];
    }



    protected void onPostExecute(Integer jobID) {
        Log.i("JobSchedulerTest","Job Finished!");
        jobFinished(mJobParameters, true);
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        mLastLocation=LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        Log.i("JobSchedulerTest", "on start job: " + mJobParameters.getJobId() + "," +
                DateFormat.getTimeInstance().format(new Date())+
                ",Location("+mLastLocation.getLatitude()+","+mLastLocation.getLongitude()+")");

    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    }
}


@Override
public boolean onStartJob(JobParameters jobParameters) {
    Log.i("JobSchedulerTest","Job Running!");
    mJobParameters=jobParameters;
    Integer i=new Integer(mJobParameters.getJobId());
    new GetLocation().execute(i);
    return true;

}

@Override
public boolean onStopJob(JobParameters jobParameters) {
    Log.i("JobSchedulerTest","Job Stopped!");
    return true;
}

}

我在提供这些参数的活动中调用了该作业

 private void scheduleJob() {
        ComponentName mServiceComponent = new ComponentName(this, TestJobService.class);
        JobInfo.Builder builder = new JobInfo.Builder(5,mServiceComponent);
        //builder.setMinimumLatency(5 * 1000); // wait at least
        //builder.setOverrideDeadline(50 * 1000); // maximum delay
        builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);// require unmetered network
        builder.setBackoffCriteria(5000,JobInfo.BACKOFF_POLICY_LINEAR);
        builder.setRequiresDeviceIdle(false); // we dont care if device should be idle
        builder.setRequiresCharging(false);// we don't care if the device is charging or not
        //builder.setPersisted(true);
        builder.setPeriodic(50*1000);

        JobScheduler jobScheduler = (JobScheduler)getApplication().getSystemService(Context.JOB_SCHEDULER_SERVICE);
        jobScheduler.schedule(builder.build());
    }

记录工作:

04-16 00:48:35.266 10455-10455/com.google.android.gms.location.sample.basiclocationsample I/JobSchedulerTest: Job Running!
04-16 00:48:35.293 10455-10455/com.google.android.gms.location.sample.basiclocationsample I/JobSchedulerTest: Job Finished!
04-16 00:48:37.484 2028-2951/? V/AlarmManager: sending alarm {c169dc type 3 *alarm*:android.content.jobscheduler.JOB_DELAY_EXPIRED}
04-16 00:48:37.550 2028-2028/? V/AlarmManager: done {c169dc, *alarm*:android.content.jobscheduler.JOB_DELAY_EXPIRED} [67ms]
04-16 00:48:37.555 10455-10455/com.google.android.gms.location.sample.basiclocationsample I/JobSchedulerTest: Job Running!
04-16 00:48:37.563 10455-10455/com.google.android.gms.location.sample.basiclocationsample I/JobSchedulerTest: Job Finished!
04-16 00:48:39.927 2028-2951/? V/AlarmManager: sending alarm {c169dc type 3 *alarm*:android.content.jobscheduler.JOB_DELAY_EXPIRED}
04-16 00:48:39.947 2028-2028/? V/AlarmManager: done {c169dc, *alarm*:android.content.jobscheduler.JOB_DELAY_EXPIRED} [23ms]
04-16 00:48:39.952 10455-10455/com.google.android.gms.location.sample.basiclocationsample I/JobSchedulerTest: Job Running!
04-16 00:48:39.957 10455-10455/com.google.android.gms.location.sample.basiclocationsample I/JobSchedulerTest: Job Finished!
04-16 00:48:50.078 2028-2951/? V/AlarmManager: sending alarm {c169dc type 3 *alarm*:android.content.jobscheduler.JOB_DELAY_EXPIRED}
04-16 00:48:50.114 2028-2028/? V/AlarmManager: done {c169dc, *alarm*:android.content.jobscheduler.JOB_DELAY_EXPIRED} [154ms]
04-16 00:48:50.117 10455-10455/com.google.android.gms.location.sample.basiclocationsample I/JobSchedulerTest: Job Running!
04-16 00:48:50.120 10455-10455/com.google.android.gms.location.sample.basiclocationsample I/JobSchedulerTest: Job Finished!

我的onConnected实现永远不会被触发。

1 个答案:

答案 0 :(得分:1)

您必须在清单和代码中询问位置权限才能使其正常工作。

清单

day

MainActivity类:

的OnCreate():

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />