Firebase作业服务无法在该时间范围内启动服务或运行

时间:2018-06-28 11:33:33

标签: java android

我在android应用中创建了一个firebase作业服务。 但是问题在于它没有在该时间段内运行,我将其设置为1分钟以进行测试,并且它也无法启动该服务。

我运行android调试器来跟踪它,但是一旦创建了作业,就什么也没有发生。我看不到它运行startJob。

我已遵循本教程-

https://classroom.udacity.com/courses/ud851/lessons/f5ef4e52-c485-4c85-a26a-3231c17d6154/concepts/5215ff23-343f-49a9-9684-cf8b9084cfb7

还有

https://medium.com/wiselteach/firebase-jobdispatcher-androidmonk-3e6d729ed9ce

public class AutomateJobs {
    private static final int REMINDER_MINUTES = 1;
    private static final int REMINDER_IN_SECONDS =  (int) (TimeUnit.MINUTES.toSeconds(REMINDER_MINUTES));
    private static final int FLEX_SECONDS = REMINDER_IN_SECONDS; // amount of seconds between each check

    private static final String REMINDER_JOB_TAG = "check_date_time_tag";
    private static boolean sInitialised;

    synchronized public static void runAutomateGamesCheck(@Nullable final Context context){
        if(sInitialised) return;
        Driver driver = new GooglePlayDriver(context);
        FirebaseJobDispatcher firebaseJobDispatcher = new FirebaseJobDispatcher(driver);
        Job constraintCheckDateTimeJob =  firebaseJobDispatcher.newJobBuilder()
                .setService(AutomateSendSms.class)
                .setTag(REMINDER_JOB_TAG)
                .setLifetime(Lifetime.FOREVER)
                .setRecurring(true)
                .setTrigger(Trigger.executionWindow(
                        REMINDER_IN_SECONDS,
                        REMINDER_IN_SECONDS + FLEX_SECONDS))
                .setReplaceCurrent(true) //if remade, replace the old one
                .build();
        firebaseJobDispatcher.schedule(constraintCheckDateTimeJob);
        sInitialised = true;
    }

然后在一个新的职位上,我有一份刚开始的工作:

//Also, not sure why  com.firebase.jobdispatcher.JobService does not import,
//I tried to import at the top of my code, but it does not like it unless I have
//it set like this. I do have the correct implementation within my gradle

public class AutomateSendSms extends com.firebase.jobdispatcher.JobService { 

    private GameDetailsFromDatabase gameDetailsFromDatabase = new GameDetailsFromDatabase();
    SmsManager smsManager = SmsManager.getDefault();
    private Calendar calendar;
    private int mYear, mMonth, mDay, mHour, mMinute,mHourPlusOne;
    private Cursor listOfAllDateTimesToSendText;
    private SimpleDateFormat sdfCurrentDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private SimpleDateFormat sdfCurrentDateTimePlusOne = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private SimpleDateFormat sdfCurrentDateTimeFromDB = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private Date currentDate,currentDatePlusOne,dateFromDB;
    private AsyncTask checkDateTime;

    @Override
    public boolean onStartJob(final com.firebase.jobdispatcher.JobParameters params) {
        checkDateTime =  new AsyncTask(){

            @Override
            protected Object doInBackground(Object[] objects) {
                listOfAllDateTimesToSendText = gameDetailsFromDatabase.agReturnDateTime();
                calendar = Calendar.getInstance();
                mYear = calendar.get(Calendar.YEAR);
                mMonth = calendar.get(Calendar.MONTH) + 1;
                mDay = calendar.get(Calendar.DAY_OF_MONTH);
                mHour = calendar.get(Calendar.HOUR_OF_DAY);
                mMinute = calendar.get(Calendar.MINUTE);
                calendar.add(Calendar.HOUR_OF_DAY, 1);
                mHourPlusOne = calendar.get(Calendar.HOUR_OF_DAY);
                String currentDateTime = mYear + "-" + mMonth + "-" + mDay + " " + mHour + ":" + mMinute + ":00";
                String currentDateTimePlusOneHour = mYear + "-" + mMonth + "-" + mDay + " " + mHourPlusOne + ":" + mMinute + ":00";
                try {
                    currentDate = sdfCurrentDateTime.parse(currentDateTime);
                    currentDatePlusOne = sdfCurrentDateTimePlusOne.parse(currentDateTimePlusOneHour);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                try {
                    while (listOfAllDateTimesToSendText.moveToNext()) {
                        String dateTimeFromCursor = listOfAllDateTimesToSendText.getString(listOfAllDateTimesToSendText.getColumnIndex(MainAutomateGames.AutomateGamesTable.COLUMN_DATE_TO_SEND_TEXT));
                        try {
                            dateFromDB = sdfCurrentDateTimeFromDB.parse(dateTimeFromCursor);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        if (dateFromDB.after(currentDate) && dateFromDB.before(currentDatePlusOne)) {
                            int gameID = listOfAllDateTimesToSendText.getColumnIndex(MainAutomateGames.AutomateGamesTable.COLUMN_GAME_ID);
                            sendSms(gameID);
                        }
                    }
                } catch (CursorIndexOutOfBoundsException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Object o){
                jobFinished(params, false);
            }
        };
        checkDateTime.execute();
        return true;
    }

    @Override
    public boolean onStopJob(com.firebase.jobdispatcher.JobParameters params) {
        return false;
    }

谢谢

0 个答案:

没有答案