如何将回调传递给JobService?

时间:2018-11-21 16:45:21

标签: android firebase callback firebase-job-dispatcher jobservice

我正在使用Firebase JobDispatcher for Android来调度JobService。我需要通知ui进行更新。如何将回调传递给JobService?

我的工作不受欢迎的班级

public class JobDispatcher {
private static final String REFRESH_ARTICLES_TAG = "refresh_articles";


private static FirebaseJobDispatcher firebaseJobDispatcher;

private JobDispatcher() {
}

public static void init(Context context) {
    firebaseJobDispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));
}

private static FirebaseJobDispatcher getDispatcher(Context context) {
    if (firebaseJobDispatcher == null)
        init(context);

    return firebaseJobDispatcher;
}


public static void scheduleRefreshArticles(Context context, RequestCallbacks callbacks) {
    Class<RefreshItemsJobService> refreshItemsJobServiceClass = RefreshItemsJobService.class;
    Bundle args = new Bundle ();
    //args.putSerializable(RefreshItemsJobService.KEY_CALLBACKS, callbacks);
    Job sendNotificationJob = getDispatcher(context).newJobBuilder()
            .setService((RefreshItemsJobService.class))
            .setTag(REFRESH_ARTICLES_TAG)
            .setLifetime(Lifetime.UNTIL_NEXT_BOOT)
            .setRecurring(true)
            .setTrigger(Trigger.executionWindow(10, 20))
            .setReplaceCurrent(true)
            .setRetryStrategy(RetryStrategy.DEFAULT_LINEAR)
            .setConstraints(Constraint.ON_ANY_NETWORK)
            //.setExtras(args)
            .build();

    getDispatcher(context).schedule(sendNotificationJob);
}

public static void stopRefreshArticles(Context context) {
    getDispatcher(context).cancel(REFRESH_ARTICLES_TAG);
}

}

和我的工作服务

public class RefreshItemsJobService extends JobService {

public final static String KEY_CALLBACKS = "callbacks";

private RequestCallbacks callbacks;

@Override
public boolean onStartJob(JobParameters job) {
    Log.e("job_refresh", "started");
    callbacks = (RequestCallbacks) job.getExtras().getSerializable(KEY_CALLBACKS);
        if (callbacks != null)
            Api.checkForNewArticles(callbacks);
    return false;
}

@Override
public boolean onStopJob(JobParameters job) {
    return true;
}

}

我试图通过捆绑传递回调,但是它引发了异常异常。

0 个答案:

没有答案