【问题标题】:Background Service using thread使用线程的后台服务
【发布时间】:2018-05-29 12:46:50
【问题描述】:
  1. 即使应用程序关闭,我如何将位置发送到服务器
  2. 有没有代码请分享给我。
  3. 提前致谢

【问题讨论】:

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


    【解决方案1】:

    由于您的知识,我们无法直接为您提供代码,我们建议您逐步或一些指导您的代码快照。

    。实施 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?"
        }
    }
    

    。最后,Schedule Job-like 波纹管

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

    【讨论】:

    • 感谢您的回复。如果有其他可能的代码,请建议我。
    • 我建议 JobSchedular 因为在 Orea android 之后不直接支持使用 startService() 运行服务。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    • 1970-01-01
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多