即使应用程序处于后台,我也会从location service
调用JobSchedular
来使其正常工作。
这是我安排LocationService
JobInfo jobInfo;
JobScheduler jobScheduler;
ComponentName componentName= new ComponentName(this, LocationUpdateService.class);
JobInfo.Builder builder= new JobInfo.Builder(11, componentName);
builder.setPeriodic(900000);
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
builder.setPersisted(true);
jobInfo= builder.build();
jobScheduler= (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
jobScheduler.schedule(jobInfo);
现在它调用LocationUpdateService
,并执行onCreate()
和onStartJob
方法,但不会在onLocationChanged()
中执行oreo
。这在Nougat
和lollipop
设备
将用户位置发送到服务的所有逻辑都在onLocationChanged()
,如果我将逻辑放在onStartJob()
中,我无法获得用户{{} 1}}。
lat/lng
我也尝试过在public class LocationUpdateService extends JobService implements
LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
IServiceResponse {
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreateCalled: ");
if (isGooglePlayServicesAvailable()) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
}
@Override
public boolean onStartJob(JobParameters params) {
Log.i(TAG, "onStartCommand: ");
return true;
}
@Override
public boolean onStopJob(JobParameters params) {
Log.d("JobStopped", "JobStopped");
return true;
}
@Override
public void onLocationChanged(Location location) {
}
中获取用户的lat/lng
,但它返回了我(0,0)onStartJob
lat/lng
我正在使用Location location= new Location("");
location.getLongitude()
版本play-services