2017-06-07 13 views
2

Firebase Jobdispatcherを実装する際、トリガ時間を10〜20秒に指定しています。これは、ジョブをスケジュールするために私のコードです:指定されたウィンドウ内でFirebase jobdispatcherが起動しない

public static void scheduleCompatibleJob(Context context) { 
    FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context)); 
    Job myJob = dispatcher.newJobBuilder() 
      .setService(DataTrackerJobService.class) // the JobService that will be called 
      .setTag("api_trigger") 
      .setRecurring(true) 
      .setLifetime(Lifetime.FOREVER) 
      .setTrigger(Trigger.executionWindow(10, 20)) // 10 to 20 seconds 
      .setReplaceCurrent(true) 
      .setRetryStrategy(RetryStrategy.DEFAULT_LINEAR) 
      .setConstraints(Constraint.ON_ANY_NETWORK) 
      .build(); 

    dispatcher.mustSchedule(myJob); 
} 

とサービスクラス:

public class DataTrackerJobService extends JobService { 
@Override 
public boolean onStartJob(final JobParameters job) { 
    Log.e("start job", "started " + job); 
    (new Handler()).postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      Log.e("handler", "run"); 
      jobFinished(job, true); 
     } 
    }, 2000); 
    return true; 
} 

@Override 
public boolean onStopJob(JobParameters job) { 
    Log.e("stop job", "stopped " + job); 
    return false; 
} 

}

仕事ディスパッチャが実行されているが、logcatの時間が正しくありません。毎回の仕事の再スケジューリングで、時間のギャップは増加し続け、決して10〜20秒の間にはなかった。

06-07 11:19:16.429 26174から26174/com.example.jobdispatcher E /ジョブ開始:19:[email protected] 06-07 11を開始しました18.432 26174から26174 /com.example.jobdispatcher E/handler:run 06-07 11:20:16.436 26174-26174/com.example.jobdispatcher E /開始ジョブ:started [email protected] 06-07 11: 20:18.438 26174-26174/com.example.jobdispatcher E/handler:run 06-07 11:21:58.519 26174-26174/com.example.jobdispatcher E /開始ジョブ:started [email protected] 06-07 11:22:00.520 26174-26174/com.example.jobdispatcher E/handler:run 06-07 11:23:40.602 26174-2 6174/com.example.jobdispatcher E /開始ジョブ:started [email protected] 06-07 11:23:42.605 26174-26174/com.example.jobdispatcher E/handler:run 06-07 11 :25:52.642 26174-26174/com.example.jobdispatcher E /開始ジョブ:started [email protected] 06-07 11:25:54.644 26174-26174/com.example.jobdispatcher E/handler:実行 06-07 11:28:52.652 26174-26174/com.example.jobdispatcher E /開始ジョブ:started [email protected] 06-07 11:28:54.655 26174-26174/com.example .jobdispatcher E/handler:run 06-07 11:32:04.688 26174-26174/com.example.jobdispatcher E /開始ジョブ:started [email protected] 06-07 11:32:06.690 26174 -26174/com.example.jobdispatcher E/handler:ru n

logcatで時刻を確認してください。私はこの中でどこが間違っているのか、それともこの方法でしか動かないのですか?基本的には24時間の時間間隔で実装したいと思っていますが、これが動作していれば、トリガー時に指定された時間の2倍後にジョブが呼び出されるのだろうかと思います。

答えて

4

トリガークラスのソースを見ると、指定した時間にジョブが実行されることが確実でないことがわかります。

/** 
* Creates a new ExecutionWindow based on the provided time interval. 
* 
* @param windowStart The earliest time (in seconds) the job should be 
*     considered eligible to run. Calculated from when the 
*     job was scheduled (for new jobs) or last run (for 
*     recurring jobs). 
* @param windowEnd The latest time (in seconds) the job should be run in 
*     an ideal world. Calculated in the same way as 
*     {@code windowStart}. 
* @throws IllegalArgumentException if the provided parameters are too 
*         restrictive. 
*/ 
public static JobTrigger.ExecutionWindowTrigger executionWindow(int windowStart, int windowEnd) { 
    if (windowStart < 0) { 
     throw new IllegalArgumentException("Window start can't be less than 0"); 
    } else if (windowEnd < windowStart) { 
     throw new IllegalArgumentException("Window end can't be less than window start"); 
    } 

    return new JobTrigger.ExecutionWindowTrigger(windowStart, windowEnd); 
} 

次の繰り返し作業は、前の作業が終了したときにのみ開始されることにも注意してください。だからあなたの仕事が長くて高価なときは、次の実行時間は予期せぬことがあります。時間のかかる作業の場合、SimpleJobServiceを拡張する必要があります。私は、適切なトリガーを作成し、私のutilのメソッドを使用しています繰り返しタスクを作成するための

public class JobDispatcherUtils { 
    public JobTrigger periodicTrigger(int frequency, int tolerance) { 
     return Trigger.executionWindow(frequency - tolerance, frequency); 
    } 
} 

使用法:

class DataTrackerJobService extends SimpleJobService { 
    // ... 
} 

public static void scheduleCompatibleJob(Context context) { 
    FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context)); 
    Job myJob = dispatcher.newJobBuilder() 
      .setService(DataTrackerJobService.class) // the JobService that will be called 
      .setTag("api_trigger") 
      .setRecurring(true) 
      .setLifetime(Lifetime.FOREVER) 
      .setTrigger(JobDispatcherUtils.periodicTrigger(20, 1)) // repeated every 20 seconds with 1 second of tollerance 
      .setReplaceCurrent(true) 
      .setRetryStrategy(RetryStrategy.DEFAULT_LINEAR) 
      .setConstraints(Constraint.ON_ANY_NETWORK) 
      .build(); 

    dispatcher.mustSchedule(myJob); 
} 
+0

がmichal..itが働いてありがとうございます! – user3099103

関連する問題