I'm using jobScheduler to get background location updates. But each time the job is scheduled, FusedLocationProviderClient is null. Why is that? I've checked if(FusedLocationProviderClient == null) condition and each time the job is scheduled, the code under it runs (that means fusedLocationProviderClient is null after it is initialized.) Please have a look at the code below. Moreover locationAvailability is often false, hence onLocationResult is not called that gives null location value. How can I optimize FusedLocationProviderClient. One more thing, are fusedLocationProviderClient always being null and locationAvailability giving false related?
@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);
}
The log values in the above code is shown below:
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]