我试图在应用程序关闭时的特定时间间隔获取位置,为此我尝试使用 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 实现从未被解雇。