【问题标题】:Firebase Job Dispatcher not triggering everytime when connected to internetFirebase Job Dispatcher 每次连接到互联网时都不会触发
【发布时间】:2018-12-25 15:06:40
【问题描述】:

以下是我用来安排作业以在我每次连接到互联网时触发的代码

FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));
                    Job job = dispatcher.newJobBuilder()
                    //persist the task across boots
                    .setLifetime(Lifetime.FOREVER)
                    //.setLifetime(Lifetime.UNTIL_NEXT_BOOT)
                    //call this service when the criteria are met.
                    .setService(ScheduledJobService.class)
                    //unique id of the task
                    .setTag("UniqueTagForYourJob")
                    //don't overwrite an existing job with the same tag
                    .setReplaceCurrent(false)
                    // We are mentioning that the job is periodic.
                    .setRecurring(true)
                    // Run between 30 - 60 seconds from now.
                    .setTrigger(Trigger.executionWindow(3, 5))
                    // retry with exponential backoff
                    .setRetryStrategy(RetryStrategy.DEFAULT_LINEAR)
                    //.setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
                    //Run this job only when the network is available.
                    .setConstraints(Constraint.ON_ANY_NETWORK)
                    .build();
                    dispatcher.mustSchedule(job);

以下是 ScheduleJobService 的代码,用于在作业执行时以当前日期时间触发随机通知(仅用于测试目的)

package com.labstract.lest.wallistract;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.RingtoneManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.support.v7.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.firebase.jobdispatcher.JobParameters;
import com.firebase.jobdispatcher.JobService;
import com.labstract.lest.wallistract.FullScreenViewSlider.FullScreenActivity;
import com.labstract.lest.wallistract.GridActivities.Image;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class ScheduledJobService extends JobService {
 @Override
 public boolean onStartJob(JobParameters job) {
  Log.d("ScheduledJobService", "Job called");
  Toast.makeText(getApplicationContext(), "hi", Toast.LENGTH_LONG).show();
  ConnectivityManager connectivity = (ConnectivityManager) getApplicationContext()
   .getSystemService(Context.CONNECTIVITY_SERVICE);
  NetworkInfo networkInfo = connectivity.getActiveNetworkInfo();
  int random = (int)(Math.random() * 50 + 1);
  if (networkInfo.isConnected()) {
   Date currentTime = Calendar.getInstance().getTime();
   Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_LONG).show();
   NotificationCompat.Builder builders = new NotificationCompat.Builder(getApplicationContext());
   Notification notifications = builders.setContentTitle("Wallistract")
    .setContentText(currentTime.toString())
    .setAutoCancel(true)
    .setPriority(Notification.PRIORITY_HIGH)
    .setSmallIcon(R.mipmap.ic_launcher)
    .build();
   NotificationManager notificationManagers = (NotificationManager) getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
   notificationManagers.notify(random, notifications);
  }
  return true;
 }


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

我的问题是我的代码有什么问题,因为每次手机连接到互联网时它都没有触发,是它的参数

.setTrigger(Trigger.executionWindow(3, 5))

.setConstraints(Constraint.ON_ANY_NETWORK)

还有什么?请帮忙。

【问题讨论】:

    标签: java android job-scheduling android-jobscheduler firebase-job-dispatcher


    【解决方案1】:

    我不确定您是否使用了正确的工具来完成这项工作。 Firebase Job Dispatcher 适用于您希望每 X 小时/天运行一次任务且对充电、互联网等有要求的用例。

    您已经描述了每次设备连接到互联网时都希望执行代码,而 Job Dispatcher 不能很好地解决这个问题。举个简单的例子,如果您快速连接 + 断开连接,Job Dispatcher 将只运行一次,然后假定其任务已完成。此外,像您列出的那样非常短的时间也不可靠。

    针对您的情况,另一种方法是在清单中注册 android.net.conn.CONNECTIVITY_CHANGE 广播接收器,然后在网络状态发生变化时由操作系统通知。这更可靠,也更容易实现。

    This answer 定义了设置广播接收器并检查其中的互联网状态的过程。代码并不理想,但它是一个起点。

    如果您只是想以比链接答案更有效的方式监控应用内的状态,我也有 previously created an example repository

    【讨论】:

      猜你喜欢
      • 2017-05-17
      • 1970-01-01
      • 1970-01-01
      • 2020-01-12
      • 1970-01-01
      • 2019-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多