簡體   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