我正在为我的工作开发一款应用。它的主要目标是验证工人是否在他们原本应该去的地方。因此,他们在一品脱销售中签入,并立即触发跟踪工作,每小时获取用户的位置。当他们离开这个地方时,工作就会停止。
我正在使用JobScheduler,因为我读到这是处理这个问题的最佳方法,而且它的工作属性很好,所以这很好。问题是,位置并不总是准确的,这是必须,因为工人的老板需要验证他们是否在他们的销售点。
有许多用户在POS内部的情况,但应用显示他/她离允许区域几米远。 (我们使用30米半径作为"允许区域")。因此,如果应用程序未显示可靠信息,我们的客户将不再信任我们的应用程序。
这是我的代码(我只显示位置内容)我将此google sample作为参考。 :
//Package and imports...
public class TrackingJob extends JobService{
// Some variables...
private static final long UPDATE_INTERVAL_IN_MILLISECONDS = 5000;
private static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 2;
private LocationCallback mLocationCallback;
private Location mLastLocation;
private LocationRequest mLocationRequest;
private LocationSettingsRequest mLocationSettingsRequest;
private FusedLocationProviderClient mFusedLocationProviderClient;
@Override
public boolean onStartJob(JobParameters jobParameters) {
createLocationCallback(jobParameters);
createLocationRequest();
buildLocationSettingsRequest();
startLocationServices();
return true; // Async stuff will be done, so true is returned
}
@Override
public boolean onStopJob(JobParameters jobParameters) { return false; }
private void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
private void createLocationCallback(final JobParameters jobParameters) {
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
mLastLocation = locationResult.getLastLocation();
// Save data locally, and then, upload it to server
saveLocalData(jobParameters.getExtras(), true);
syncData(jobParameters);
// We stop locationProvider here, because we got a location already
mFusedLocationProviderClient.removeLocationUpdates(mLocationCallback);
}
};
}
private void buildLocationSettingsRequest(){
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(mLocationRequest);
mLocationSettingsRequest = builder.build();
}
@SuppressLint("MissingPermission") // Permissions WILL BE accepted before the job is called
private void startLocationServices() {
SettingsClient mSettingsClient = LocationServices.getSettingsClient(this);
mSettingsClient.checkLocationSettings(mLocationSettingsRequest)
.addOnSuccessListener(new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
mFusedLocationProviderClient =
LocationServices.getFusedLocationProviderClient(TrackingJob.this);
// We ask for location updates
mFusedLocationProviderClient.requestLocationUpdates(
mLocationRequest,
mLocationCallback,
Looper.myLooper()
);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// If problem can be solved by user, we send a push noty
}
});
}
private void saveLocalData(PersistableBundle bundle, boolean gps){
// We save data to REALM
}
private void syncData(final JobParameters jobParameters){
// We get data to sync from REALM
// If data was succesfully synced, we delete it from REALM
// We stop the job
jobFinished(jobParameters, false);
}
}
我做错了吗?获取用户位置有更好,更准确的方法吗?这是与手机有关的&#34;问题
在使用这个新的FusedLocationProvider之前,我使用的是GoogleApiClient connectionCallbacks和LocationListeners,准确性也是一个问题。