gpt4 book ai didi

android - 使用 JobScheduler API 进行位置更新

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:32:52 24 4
gpt4 key购买 nike

下面是我使用 FireBaseJobDispatcher 开始工作的演示代码。

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
Job job=createJob(dispatcher);
dispatcher.schedule(job);


}

public static Job createJob(FirebaseJobDispatcher dispatcher){
Job job = dispatcher.newJobBuilder()
// persist the task across boots
.setLifetime(Lifetime.FOREVER)
// Call this service when the criteria are met.
.setService(ScheduledJobService.class)
// unique id of the task
.setTag("LocationJob")
// We are mentioning that the job is not periodic.
.setRecurring(true)
// Run between 30 - 60 seconds from now.
.setTrigger(Trigger.executionWindow(10,20))
//Run this job only when the network is avaiable.
.setConstraints(Constraint.ON_ANY_NETWORK)
.build();
return job;
}
}

下面是演示服务

public class ScheduledJobService extends JobService implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener,LocationListener {
private GoogleApiClient mGoogleApiClient;
LocationRequest mLocationRequest;

@Override
public boolean onStartJob(JobParameters job) {
Log.d("token","Start Job Called");
setUpLocationClientIfNeeded();
mLocationRequest = LocationRequest.create();
// Use high accuracy
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(30000);
return true;
}

@Override
public boolean onStopJob(JobParameters job) {
Log.d("token","stopped");
return true;
}

@Override
public void onConnected(@Nullable Bundle bundle) {
LocationServices.FusedLocationApi.requestLocationUpdates(this.mGoogleApiClient,
mLocationRequest, this); // This is the changed line.
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

private void setUpLocationClientIfNeeded()
{
if(mGoogleApiClient == null)
buildGoogleApiClient();
}

protected synchronized void buildGoogleApiClient() {
this.mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
this.mGoogleApiClient.connect();
}

@Override
public void onLocationChanged(Location location) {
Log.d("token",location.getLatitude()+""+location.getLongitude());
}
}

根据收到的新位置,我正在更新 map 中的位置,一旦用户到达目的地位置,我想完成工作,所以一旦用户到达目的地并释放资源,我如何停止这项工作。

最佳答案

您可以调用jobFinished (JobParameters params, boolean needsReschedule)onLocationChanged(Location 位置) 中。类似下面的内容

@Override
public void onLocationChanged(Location location) {
Log.d("token",location.getLatitude()+""+location.getLongitude());
if (location.getLatitude() == /*your value here*/ && location.getLongitude() == /*your value here */) {
jobFinished(null, false);
}
}

希望这对您有所帮助!

关于android - 使用 JobScheduler API 进行位置更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43752293/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com