我想构建一个通知应用程序,该应用程序每15分钟提供一次通知。 构建此应用程序时,我使用FirebaseJobDispatcher作业调度程序。但是当我使用newJobBuilder()构建作业时。但是这一次当我使用setService()方法时,我得到了编译时错误。我无法修复它。我已经在网络上搜索了合适的解决方案,但没有发现任何有用的方法。请帮助我。
setService
(java.lang.Class) 无法在Builder中应用 至 (java.lang.Class)
package com.example.android.background.sync;
import android.app.job.JobParameters;
import android.app.job.JobService;
import android.content.Context;
import android.os.AsyncTask;
import com.firebase.jobdispatcher.RetryStrategy;
public class WaterReminderFirebaseJobService extends JobService {
private AsyncTask mBackgroundTask;
/**
* The entry point to your Job. Implementations should offload work to another thread of
* execution as soon as possible.
*
* This is called by the Job Dispatcher to tell us we should start our job. Keep in mind this
* method is run on the application's main thread, so we need to offload work to a background
* thread.
*
* @return whether there is more work remaining.
*/
@Override
public boolean onStartJob(final JobParameters jobParameters) {
// AsyncTask called mBackgroundTask.
// Here's where we make an AsyncTask so that this is no longer on the main thread
mBackgroundTask = new AsyncTask() {
// COMPLETED (6) Override doInBackground
@Override
protected Object doInBackground(Object[] params) {
// COMPLETED (7) Use ReminderTasks to execute the new charging reminder task you made, use
// this service as the context (WaterReminderFirebaseJobService.this) and return null
// when finished.
Context context = WaterReminderFirebaseJobService.this;
ReminderTasks.executeTask(context, ReminderTasks.ACTION_CHARGING_REMINDER);
return null;
}
@Override
protected void onPostExecute(Object o) {
// COMPLETED (8) Override onPostExecute and called jobFinished. Pass the job parameters
// and false to jobFinished. This will inform the JobManager that your job is done
// and that you do not want to reschedule the job.
/*
* Once the AsyncTask is finished, the job is finished. To inform JobManager that
* you're done, you call jobFinished with the jobParamters that were passed to your
* job and a boolean representing whether the job needs to be rescheduled. This is
* usually if something didn't work and you want the job to try running again.
*/
jobFinished(jobParameters, false);
}
};
mBackgroundTask.execute();
return true;
}
/**
* Called when the scheduling engine has decided to interrupt the execution of a running job,
* most likely because the runtime constraints associated with the job are no longer satisfied.
*
* @return whether the job should be retried
* @see RetryStrategy
*/
@Override
public boolean onStopJob(JobParameters jobParameters) {
if (mBackgroundTask != null) mBackgroundTask.cancel(true);
return true;
}
}
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.background.sync;
import android.content.Context;
import android.support.annotation.NonNull;
import com.firebase.jobdispatcher.Constraint;
import com.firebase.jobdispatcher.Driver;
import com.firebase.jobdispatcher.FirebaseJobDispatcher;
import com.firebase.jobdispatcher.GooglePlayDriver;
import com.firebase.jobdispatcher.Job;
import com.firebase.jobdispatcher.Lifetime;
import com.firebase.jobdispatcher.Trigger;
import java.util.concurrent.TimeUnit;
public class ReminderUtilities {
private static final int REMINDER_INTERVAL_MINUTES=1;
private static final int REMINDER_INTERVAL_SECONDS=(int)(TimeUnit.MINUTES.toSeconds(REMINDER_INTERVAL_MINUTES));
private static final int SYNC_FLEXTIME_SECONDS=REMINDER_INTERVAL_SECONDS;
private static final String REMINDER_JOB_TAG="hydration_reminder_tag";
private static boolean sInitialized;
synchronized public static void scheduleChargingReminder(@NonNull final Context context){
if(sInitialized) return;
Driver driver=new GooglePlayDriver(context);
FirebaseJobDispatcher dispatcher=new FirebaseJobDispatcher(driver);
/* Create the Job to periodically create reminders to drink water */
Job constraintReminderJob = dispatcher.newJobBuilder()
/* The Service that will be used to write to preferences */
.setService(WaterReminderFirebaseJobService.class)
/*
* Set the UNIQUE tag used to identify this Job.
*/
.setTag(REMINDER_JOB_TAG)
/*
* Network constraints on which this Job should run. In this app, we're using the
* device charging constraint so that the job only executes if the device is
* charging.
*
* In a normal app, it might be a good idea to include a preference for this,
* as different users may have different preferences on when you should be
* syncing your application's data.
*/
.setConstraints(Constraint.DEVICE_CHARGING)
/*
* setLifetime sets how long this job should persist. The options are to keep the
* Job "forever" or to have it die the next time the device boots up.
*/
.setLifetime(Lifetime.FOREVER)
/*
* We want these reminders to continuously happen, so we tell this Job to recur.
*/
.setRecurring(true)
/*
* We want the reminders to happen every 15 minutes or so. The first argument for
* Trigger class's static executionWindow method is the start of the time frame
* when the
* job should be performed. The second argument is the latest point in time at
* which the data should be synced. Please note that this end time is not
* guaranteed, but is more of a guideline for FirebaseJobDispatcher to go off of.
*/
.setTrigger(Trigger.executionWindow(
REMINDER_INTERVAL_SECONDS,
REMINDER_INTERVAL_SECONDS + SYNC_FLEXTIME_SECONDS))
/*
* If a Job with the tag with provided already exists, this new job will replace
* the old one.
*/
.setReplaceCurrent(true)
/* Once the Job is ready, call the builder's build method to return the Job */
.build();
/* Schedule the Job with the dispatcher */
dispatcher.schedule(constraintReminderJob);
/* The job has been initialized */
sInitialized = true;
}
}