[英]Android Work manager get triggered everytime I open my app
I have made a periodic work manager and its misbehaving heavily.我做了一个周期性的工作经理,它的行为举止很严重。 Instead of doing a work in the set interval it executes it every minute.它不是在设定的时间间隔内执行一项工作,而是每分钟执行一次。 And it also gets triggered everytime i open the app.每次我打开应用程序时都会触发它。 This means if I open my app 50 times in a minute, It does the work 50 times.这意味着如果我在一分钟内打开我的应用程序 50 次,它会完成 50 次工作。 Here is my code这是我的代码
public class UpdateUserDataService extends Worker {
public UpdateUserDataService(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
}
@RequiresApi(api = Build.VERSION_CODES.O)
@NonNull
@Override
public Result doWork() {
FirebaseMessaging.getInstance().getToken().addOnSuccessListener(s -> {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Update");
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy_h:mm a", Locale.getDefault());
Calendar c = Calendar.getInstance();
String currentDate = dateFormat.format(c.getTime());
JobFooter jobFooter = new JobFooter(currentDate, s);
reference.child(dateFormat.format(new Date())).setValue(jobFooter);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
notificationIntent.putExtra("Notification", true);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(getApplicationContext(), 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationHelper notificationHelper = new NotificationHelper(getApplicationContext(), defaultSoundUri);
Notification.Builder notificationBuilder = notificationHelper.getNotification("Title", currentDate, intent);
notificationHelper.notify(10, notificationBuilder);
});
return Result.success();
}
}
And in my main activity under oncreate method在我的主要活动中 oncreate 方法
Constraints constraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.setRequiresBatteryNotLow(true)
.build();
PeriodicWorkRequest periodicWorkRequest = new PeriodicWorkRequest.Builder(
UpdateUserDataService.class, 15, TimeUnit.MINUTES)
.setConstraints(constraints)
.build();
WorkManager.getInstance(this).enqueue(periodicWorkRequest);
All I want is my code to execute after every 15 minutes followings OS norms and constraints and irrespective of my app getting opened or not.我想要的只是我的代码在遵循操作系统规范和约束后每 15 分钟执行一次,而不管我的应用程序是否打开。
String EXPIRY_CHECK = "unique_worker_name";
Constraints constraints = new Constraints.Builder()
.setRequiresBatteryNotLow(true)
.build();
PeriodicWorkRequest periodicWorkRequest = new PeriodicWorkRequest
.Builder(UpdateUserDataService.class, 1, TimeUnit.DAYS)
.setConstraints(constraints).build();
WorkManager.getInstance(this)
.enqueueUniquePeriodicWork(EXPIRY_CHECK, ExistingPeriodicWorkPolicy.KEEP, periodicWorkRequest);
This will check if an already existing worker is initialised or not and will not launch a new worker if it is already running.这将检查一个已经存在的工作人员是否已初始化,如果它已经在运行,则不会启动一个新工作人员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.