我尝试在应用程序关闭时以一定的时间间隔获取位置,为此,我尝试使用 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;
}
}
我已在提供这些参数的 Activity 中调用该作业
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 实现从未被触发。
最佳答案
您必须在 list 和代码中请求位置权限才能使其正常工作。
list
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
MainActivity 类:
oncreate():
private static final int MY_PERMISSIONS_REQUEST_FINE_LOCATION = 111;
if (ContextCompat.checkSelfPermission(MainActivity.this, // Check for permission
android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
this, // Activity
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_FINE_LOCATION);
}
关于java - 在使用 Job Scheduler API 的作业服务中使用 GoogleApiClient - 缺少回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43430452/