java - Firebase Job Dispatcher 每次连接到互联网时都不会触发
问题描述
下面是我用来安排作业以在我每次连接到互联网时触发的代码
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)
或者
还有什么其他的吗?请帮忙 。
解决方案
我不确定您是否使用了正确的工具来完成这项工作。Firebase Job Dispatcher 适用于您希望每 X 小时/天运行一次任务的用例,并且需要充电、互联网等。
您已经描述了希望在每次设备连接到 Internet 时执行代码,而 Job Dispatcher 不能很好地工作。举个简单的例子,如果您快速连接 + 断开连接,Job Dispatcher 将只运行一次,然后假定其任务已完成。此外,像您列出的那样非常短的时间是不可靠的。
您的情况的另一种方法是android.net.conn.CONNECTIVITY_CHANGE
在清单中注册广播接收器,然后在网络状态发生变化时由操作系统通知。这更可靠,也更容易实现。
这个答案定义了设置广播接收器并检查其中的互联网状态的过程。代码并不理想,但它是一个起点。
如果您只想以比链接答案更有效的方式监视应用程序内部的状态,我之前还创建了一个示例存储库。
推荐阅读
- python-3.x - 如何根据 Keras 中的输出概率将其区分为 True 或 False?
- mongodb - yum intsall mongdb [Errno 256] 没有更多镜像可以尝试
- selenium - WebDriver cannot be resolved to a type ChromeDriver cannot be resolved to a type
- javascript - 当工具提示出现时,顶部的 Chart.JS 值消失
- python - Use filename on dataframe and match it with another list and store the list element
- javascript - 最初不存在的 JQuery 选择元素
- javascript - 如何在 JavaScript ES6 中功能性地遍历矩阵?
- batch-file - 为什么即使启用延迟扩展后,变量名也不会被感叹号包围输出值?
- neural-network - “打破对称性”是什么意思?在神经网络编程的背景下?
- html - 将图像放入单元格中