FirebaseJobDispatcher的JobService不再重试

时间:2019-12-12 10:25:09

标签: android firebase-job-dispatcher jobservice

我试图一次又一次地运行服务,直到活动被破坏,但它仅调用一次,并且也没有停止并再次执行。 jobFinished(jobParameters,true);然后也不会再次运行。是否需要setTrigger? 或必须广播接收器。

这是我的代码;

服务:

public class GetAllChatService extends JobService {

private JobParameters jobParameters;


@Override
public boolean onStartJob(JobParameters job) {

    Log.e("PARENT", "Message sending job started");
    jobParameters = job;
    String json="r";
    if (jobParameters.getExtras()!=null){
         json=   jobParameters.getExtras().getString("json");
        Log.e("json", json);
        final ChatMainRepository chatMainRepository = new ChatMainRepository(getApplication());
        RequestBody body = RequestBody.create(MediaType.parse("application/json"), json);
        new ApiClient().getClient().getAllChat(body)
                .map(new Function<GetChatMainPojo, List<ChatMainModel>>() {
                    @Override
                    public List<ChatMainModel> apply(GetChatMainPojo getAdminCircularParentPOJO) throws Exception {
                        return getAdminCircularParentPOJO.getData();
                    }
                })
                .flatMapIterable(new Function<List<ChatMainModel>, Iterable<ChatMainModel>>() {
                    @Override
                    public Iterable<ChatMainModel> apply(List<ChatMainModel> data) throws Exception {
                        return data;
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DisposableObserver<ChatMainModel>() {
                    @Override
                    public void onNext(ChatMainModel message) {
                         Log.e("message",""+message.getMessage());
                        chatMainRepository.insertOrUpdateAsyncTask(message);
                    }

                    @Override
                    public void onError(Throwable throwable) {
                        Log.e("TAG", throwable.getLocalizedMessage());
                      jobFinished(jobParameters, true);
                    }

                    @Override
                    public void onComplete() {
                        Log.e("onComplete", "onComplete");
                        jobFinished(jobParameters, true);
                       // dataSendMessageToActivity();
                    }
                });
    }



    return true;
}

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


private void dataSendMessageToActivity() {
    Intent intent = new Intent(AppConstants.NOTIFY_SENDSERVICE);
    sendLocalTextMessageBroadcast(intent);
}
private void sendLocalTextMessageBroadcast(Intent intent) {
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

}

活动启动服务:

  public void startService(String json){

    Log.d("startService","startService");
  Bundle bundle = new Bundle();
    bundle.putString("json", json);

    Job myJob = dispatcher.newJobBuilder()
            .setService(GetAllChatService.class)
            .setLifetime(Lifetime.FOREVER)
            .setTag(TAGSERVIE)
            .setReplaceCurrent(true)
           .setExtras(bundle)
            .setRetryStrategy(RetryStrategy.DEFAULT_LINEAR)
            .setConstraints(
                    Constraint.ON_ANY_NETWORK
            )
            .build();

    dispatcher.mustSchedule(myJob);
}

0 个答案:

没有答案