Firebase作业服务无法启动作业

时间:2018-07-02 10:39:24

标签: java android

我在android应用中创建了一个firebase作业服务。

但是由于某种原因,它不会执行启动作业。

我运行android调试器来跟踪它,但是一旦创建了作业,就什么也没有发生。但是由于某种原因,它不会执行启动作业。

我已遵循本教程-

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;
    }

谢谢

1 个答案:

答案 0 :(得分:0)

我在没有Google Play服务的模拟器上运行此程序。当我在真正的设备上尝试时,它可以工作。 您必须选择支持Google Play的图片