, что для этой цели нам следует использовать requestLocationUpdates (запрос LocationRequest, PendingIntent callbackIntent). Я планирую работу в своей основной активности следующим образом:
Код: Выделить всё
final ComponentName serviceComponent = new ComponentName(this, LocationJobService.class);
final JobScheduler jobScheduler;
final JobInfo.Builder builder = new JobInfo.Builder(LocationJobService.LOCATION_JOB_SERVICE_ID,
serviceComponent);
builder.setPeriodic(JobInfo.getMinPeriodMillis(), JobInfo.getMinFlexMillis());
builder.setPersisted(true);
final JobInfo jobInfo = builder.build();
jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
if (jobScheduler != null)
{
if(jobScheduler.schedule(jobInfo) == JobScheduler.RESULT_SUCCESS)
Log.d(TAG, String.format("Job %s successfully scheduled", LocationJobService.LOCATION_JOB_SERVICE_ID));
else
Log.e(TAG, "Job schedule failed!");
}
Код: Выделить всё
private static final String TAG = "Class " + LocationJobService.class.getSimpleName();
public static final int LOCATION_JOB_SERVICE_ID = 1001;
private FusedLocationProviderClient client;
@Override
public boolean onStartJob(final JobParameters params)
{
//some logic before initializing location update
initializeLocationUpdate(getApplicationContext());
}
private void initializeLocationUpdate(Context context)
{
client = LocationServices.getFusedLocationProviderClient(context);
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(3000);
locationRequest.setFastestInterval(1000);
client.requestLocationUpdates(locationRequest, pendingIntent)
.addOnCompleteListener(new OnCompleteListener()
{
@Override
public void onComplete(@NonNull Task task)
{
Log.d(TAG, "onComplete " + task.getResult());
}
}).addOnFailureListener(new OnFailureListener()
{
@Override
public void onFailure(@NonNull Exception e)
{
Log.d(TAG, "onFailure");
}
});
}
Подробнее здесь: https://stackoverflow.com/questions/481 ... jobservice
Мобильная версия