我正在使用 jobScheduler 来获取后台位置更新。但是每次调度作业时,FusedLocationProviderClient 为空。这是为什么?我检查了 if(FusedLocationProviderClient == null) 条件,每次调度作业时,它下面的代码都会运行(这意味着 fusedLocationProviderClient 在初始化后为空。)请看下面的代码。此外,locationAvailability 通常是错误的,因此不会调用 onLocationResult 来给出空位置值。如何优化 FusedLocationProviderClient。还有一件事,fusedLocationProviderClient 是否总是为空,而 locationAvailability 是否与 false 相关?
@Override
public boolean onStartJob(JobParameters jobParameters) {
Log.e("onStartJob", "onStartJob");//for debug
jobP = jobParameters;
if (!checkAndRequestPermissions()) {
Toast.makeText(this, "Please provide location permission for paramount app.", Toast.LENGTH_LONG).show();
provider = null;
} else {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
}
if (mLocationRequest == null) {
Log.e("onStartJob", "LocationRequest initialized"); //for debug
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(100 * 1000);
mLocationRequest.setFastestInterval(60 * 1000);
}
if (client == null) {
Log.e("onStartJob", "client initialized"); //for debug
client = LocationServices.getFusedLocationProviderClient(this);
client.requestLocationUpdates(mLocationRequest, new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
Log.e("onLocationResult ", "onLocationResult");
onLocationChanged(locationResult.getLastLocation());
}
@Override
public void onLocationAvailability(LocationAvailability locationAvailability) {
Log.e("onLocationAvailability", locationAvailability + "");;
}
},
Looper.myLooper());
}
try {
provider = Settings.Secure.getInt(getContentResolver(), Settings.Secure.LOCATION_MODE) + "";
gpsProvider = provider;
} catch (Settings.SettingNotFoundException e) {
Log.e("provider", "gps provider error");
}
}
return true;
}
@Override
public boolean onStopJob(JobParameters jobParameters) {
Log.e("onStopJob", "onStopJob");//for debug
if (ul != null) {
ul.cancel(true);
}
return true;
}
public void onLocationChanged(Location location) {
latitude = location.getLatitude() + "";
longitude = location.getLongitude() + "";
Log.e("latitude" , latitude);
}
上述代码中的日志值如下所示:
03-15 17:09:25.889 10687-10687/com.myProject.com.jobschedulers E/onStartJob: onStartJob
03-15 17:09:25.900 10687-10687/com.myProject.com.jobschedulers E/onstartJob: client initialized
03-15 17:09:25.957 10687-10687/com.myProject.com.jobschedulers E/onLocationResult: onLocationResult
03-15 17:09:25.960 10687-10687/com.myProject.com.jobschedulers E/onLocationAvailability: LocationAvailability[isLocationAvailable: true]
03-15 17:23:26.975 10687-10687/com.myProject.com.jobschedulers E/onStartJob: onStartJob
03-15 17:23:26.993 10687-10687/com.myProject.com.jobschedulers E/onstartJob: client initialized
03-15 17:23:27.017 10687-10687/com.myProject.com.jobschedulers E/onLocationAvailability: LocationAvailability[isLocationAvailable: false]
03-15 17:41:32.672 10687-10687/com.myProject.com.jobschedulers E/onStartJob: onStartJob
03-15 17:41:32.690 10687-10687/com.myProject.com.jobschedulers E/onstartJob: client initialized
03-15 17:41:32.741 10687-10687/com.myProject.com.jobschedulers E/onLocationAvailability: LocationAvailability[isLocationAvailable: false]
03-15 17:53:17.335 10687-10687/com.myProject.com.jobschedulers E/onStartJob: onStartJob
03-15 17:53:17.351 10687-10687/com.myProject.com.jobschedulers E/onstartJob: client initialized
03-15 17:53:17.383 10687-10687/com.myProject.com.jobschedulers E/onLocationAvailability: LocationAvailability[isLocationAvailable: false]