【问题标题】:Location Updates using JobScheduler API使用 JobScheduler API 进行位置更新
【发布时间】:2017-09-30 19:51:42
【问题描述】:

下面是我使用 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());
    }
}

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

【问题讨论】:

  • 你能发布完整的代码吗..带有清单文件还有活动。

标签: android firebase-job-dispatcher


【解决方案1】:

您可以在onLocationChanged(Location location) 中致电jobFinished (JobParameters params, boolean needsReschedule)。类似于以下内容

@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);
    }
}

希望这会有所帮助!

【讨论】:

  • @Omar Al Halabi 如果框架停止我的工作,那么将调用 stopjob 并且再次运行此工作必须从那里返回 true 对吗?另外,如果我想从另一个活动或片段中取消这项工作,那么我们该怎么做,请您指导一下?
  • 是的,框架将调用onStopJob,如果返回true,作业将再次运行。另外,如果您想从另一个片段或活动中取消它,我建议将 jobdispatcher 设为单例并致电dispatcher.cancel("LocationJob");
  • @Omar Al Halabi,因为启动作业中的代码在主线程上运行,是否可以像我在上面的代码中所做的那样从那里请求位置更新
  • 一般来说不会,但是你可以将looper作为最后一个参数传递给requestLocationUpdates
  • @Omar Al Halabi 我只是从那里请求更新,并通过实施位置列表器从改变位置的方法向活动发送广播。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-02
  • 2021-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多