gpt4 book ai didi

java - 连接到互联网时,Firebase Jobdispatcher 不会每次都触发

转载 作者:行者123 更新时间:2023-11-30 05:09:51 25 4
gpt4 key购买 nike

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

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 将只运行一次,然后假定它的任务已经完成。此外,像您列出的那样非常短的时间并不可靠。

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

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

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

关于java - 连接到互联网时,Firebase Jobdispatcher 不会每次都触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53923490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com