4

JobService每个周期只跑一次时遇到了一些麻烦。

这是我简单而愚蠢的代码JobService

public class UpdateInfoJobService extends JobService {

    public final static int UPDATE_TRACKING_INFO_JOB_ID = 741;

    @Override
    public boolean onStartJob(JobParameters jobParameters) {
        notifyNewStatus("TEST");
        jobFinished(jobParameters, false);
        return false;
    }

    @Override
    public boolean onStopJob(JobParameters jobParameters) {
        scheduleNextUpdate(this);
        return false;
    }

    public static void scheduleNextUpdate(Context mContext) {

        boolean runJob = true;

        if (runJob) {
            ComponentName serviceComponent = new ComponentName(mContext, UpdateInfoJobService.class);
            JobScheduler jobScheduler = (JobScheduler) mContext.getSystemService(Context.JOB_SCHEDULER_SERVICE);

            JobInfo.Builder builder = new JobInfo.Builder(UPDATE_TRACKING_INFO_JOB_ID, serviceComponent);
            builder.setPersisted(true);
            builder.setPeriodic(30*60*1000); //every 30min

            if (jobScheduler != null)
                jobScheduler.schedule(builder.build());
        }

    }

    private void notifyNewStatus (String s) {


        // build notification
        Notification n  = new Notification.Builder(this)
                .setContentTitle(s + " :: " + Calendar.getInstance().getTime())
                .build();

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        final int notificationId = (int)System.currentTimeMillis();

        Log.d("UpdateInfoJob", "New notification with ID=" + notificationId);

        if (notificationManager != null)
            notificationManager.notify(getApplication().getPackageName(), notificationId, n);
    }
}

我在 logcat 中得到的输出:

01-14 16:33:22.591 19733-19733/com.android.myapp D/UpdateInfoJob: New notification with ID=240532138
01-14 16:43:57.831 19733-19733/com.android.myapp D/UpdateInfoJob: New notification with ID=241167372
01-14 16:49:35.102 19733-19733/com.android.myapp D/UpdateInfoJob: New notification with ID=241504645
01-14 16:49:40.032 19733-19733/com.android.myapp D/UpdateInfoJob: New notification with ID=241509577
01-14 16:57:12.982 19733-19733/com.android.myapp D/UpdateInfoJob: New notification with ID=241962532
01-14 17:05:15.197 19733-19733/com.android.myapp D/UpdateInfoJob: New notification with ID=242444743
01-14 17:05:15.327 19733-19733/com.android.myapp D/UpdateInfoJob: New notification with ID=242444876
01-14 17:10:15.507 19733-19733/com.android.myapp D/UpdateInfoJob: New notification with ID=242745058
01-14 17:15:15.847 19733-19733/com.android.myapp D/UpdateInfoJob: New notification with ID=243045395
01-14 17:20:00.277 19733-19733/com.android.myapp D/UpdateInfoJob: New notification with ID=243329827
01-14 17:20:45.007 19733-19733/com.android.myapp D/UpdateInfoJob: New notification with ID=243374556
01-14 17:25:16.077 19733-19733/com.android.myapp D/UpdateInfoJob: New notification with ID=243645621

调试日志每 5/10 分钟发送一次,而不是应有的 30 分钟发送一次。为什么会这样?我究竟做错了什么?

谢谢你的帮助!

4

0 回答 0