[英]Firebase Job Dispatcher not triggering everytime when connected to internet
以下是我用來安排作業以每次我連接到互聯網時觸發的代碼
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
廣播接收器,然后每當網絡狀態發生變化時,操作系統就會通知您。 這更加可靠,並且更易於實現。
該答案定義了設置廣播接收器並檢查其中的Internet狀態的過程。 代碼並不理想,但這是一個起點。
如果您只是想監視應用程序內部的狀態,那么我以前也創建了一個示例存儲庫 ,它比鏈接的答案更有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.