【问题标题】:PersistableBundle pass values to service and retrievePersistableBundle 将值传递给服务并检索
【发布时间】:2015-03-30 10:08:50
【问题描述】:

我正在使用 JobScheduler 并且能够运行调度程序。

现在我想使用 PersistableBundle 将一些值从我的 Activity 传递给我的 Service 类。有人可以指导如何传递值并检索它吗?这是我试图传递值的函数。

private void constructJob(){
JobInfo.Builder builder = new JobInfo.Builder(JOB_ID, new ComponentName(this, MyService.class));
PersistableBundle bundle = new PersistableBundle();
bundle.putInt("height",height);
bundle.putInt("width",width);
builder.setPeriodic(20000)
        .setExtras(bundle)
        .setPersisted(true);

mJobScheduler.schedule(builder.build());

}

【问题讨论】:

    标签: android service android-service android-jobscheduler


    【解决方案1】:

    您的代码看起来不错。您的JobService 将使用onStartJob() 调用,并在JobParameters 中传递。在JobParameters 上调用getExtras() 以让PersistableBundle 读取值:

      @Override
      public boolean onStartJob(JobParameters params) {
        PersistableBundle pb=params.getExtras();
    
        // do good stuff
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-12
      • 2017-05-25
      • 2015-02-28
      • 1970-01-01
      相关资源
      最近更新 更多