使用线程的后台服务

时间:2018-05-29 12:46:50

标签: android android-fragments android-developer-api

  1. 即使应用程序关闭,我如何向服务器发送位置
  2. 有没有代码请与我分享。
  3. 提前致谢

1 个答案:

答案 0 :(得分:0)

我们无法直接为您提供代码,因为您的知识,我们建议您使用步骤或一些代码快照来指导您。

。实施firebase Job Dispatcher Firebase JobDispatcher
。成功添加库后,会创建一个扩展JobService的服务,如下所示。

import com.firebase.jobdispatcher.JobParameters;
import com.firebase.jobdispatcher.JobService;

public class MyJobService extends JobService {
    @Override
    public boolean onStartJob(JobParameters job) {
        // Do some work here
        //getLatestLocationAndUpdateOnserver()
        //Don't miss to reschedule job
        return false; // Answers the question:z "Is there still work going on?"
    }

    @Override
    public boolean onStopJob(JobParameters job) {
        return false; // Answers the question: "Should this job be retried?"
    }
}

。最后,安排Job-like bellow

private void scheduleJob() {
        FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
        Bundle myExtrasBundle = new Bundle();
        myExtrasBundle.putString("request_sender", "Data Want to add");
        com.firebase.jobdispatcher.Job myJob = dispatcher.newJobBuilder()
                .setService(MyJobService.class) // the JobService that will be called
                .setTag("my-unique-tag")
                .setTrigger(Trigger.executionWindow(600, 610)) //this will fire after 10minute 
                .setReplaceCurrent(true)// uniquely identifies the job
                .setExtras(myExtrasBundle)
                .build();
        if (dispatcher != null) {
            dispatcher.cancel("my-unique-tag");
        }
        dispatcher.mustSchedule(myJob);
    }