繁体   English   中英

Android 每次打开我的应用程序时都会触发工作管理器

[英]Android Work manager get triggered everytime I open my app

我做了一个周期性的工作经理,它的行为举止很严重。 它不是在设定的时间间隔内执行一项工作,而是每分钟执行一次。 每次我打开应用程序时都会触发它。 这意味着如果我在一分钟内打开我的应用程序 50 次,它会完成 50 次工作。 这是我的代码

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();
    }
}

在我的主要活动中 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);

我想要的只是我的代码在遵循操作系统规范和约束后每 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);

这将检查一个已经存在的工作人员是否已初始化,如果它已经在运行,则不会启动一个新工作人员。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM