简体   繁体   English

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

[英]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.

相关问题 Android-每次我尝试开启应用程式时都会当机 - Android - App Crash everytime I try to open it 每次我打开和关闭我的应用程序时都会出现异常 - Exceptions coming everytime I open and close my app 每次我在我的Android应用程序的http post的帮助下插入一行表,插入两行 - everytime i insert a row into a table with the help of http post from my android app, two rows get inserted 我无法打开我的Android SDK管理器 - I can not open my Android SDK Manager 在工作管理器 android 中 3 天后未触发通知 - Notification not triggered after 3 days in work manager android 如何使用 Android 文件管理器打开我的应用程序文档目录 - How to open my app documents directory with Android File Manager 每当我打开应用程序时,AlarmManager都会关闭 - AlarmManager going off everytime i open the app 每当我打开应用程序时,闹钟响起 - Alarm Goes off everytime I open the App 单击Android时,如何使我的文件管理器应用程序启动文件? - how do I get my file manager app to start a file when clicked Android? 在 Android 上,我无法让 actions.intent.OPEN_APP_FEATURE 在应用程序操作测试工具之外工作 - On Android, I cannot get actions.intent.OPEN_APP_FEATURE to work outside the App Actions Test Tool
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM