简体   繁体   English

工作管理器的 doWork() 方法没有从警报管理器接收器中调用

[英]doWork() method of workmanager is not getting called from alarm manager receiver

I am using work manager Onetime Request to perform the task.我正在使用工作管理器 Onetime Request 来执行任务。 First the alarm manager gets call in given frequency and from alarm manager receiver class, work manager class gets called.首先,警报管理器以给定的频率被调用,并且从警报管理器接收器类,工作管理器类被调用。 All code was working fine, but suddenly the work manager doWork is not getting called.所有代码都运行良好,但突然没有调用工作经理 doWork。 Flow is coming till alarm manager receiver but not proceeding further.流程将持续到警报管理器接收器,但不会继续进行。

Alarm Manager Receiver class警报管理器接收器类

public class WorkAlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    final String TAG = "WorkAlarmReceiver";
    if (BuildConfig.DEBUG) {
        Log.i(TAG, "onReceive: intent=" + intent);
    }
    OneTimeWorkRequest startWork = new OneTimeWorkRequest.Builder(WorkManagerForTools.class).build();
    WorkManager.getInstance(context).enqueueUniqueWork("Validate Tool", ExistingWorkPolicy.APPEND_OR_REPLACE , startWork);

}

This Receiver is calling fine in given time interval此接收器在给定的时间间隔内调用正常

        //checking if alarm is working with pendingIntent
        Intent workIntent = new Intent(mContext,
                WorkAlarmReceiver.class)
                .setAction(Long.toString(System.currentTimeMillis()
        PendingIntent workPendingIntent = PendingIntent.getBroadcast(mContext, 1001,
                workIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        Log.e(TAG, "New data of pending intent: " + workSpecId + " local work id, " + localWorkId +
                " tools id list " + updatedMobileAppToolIds);
        if(BuildConfig.DEBUG) {
            Log.d(TAG, "New data of pending intent: " + workSpecId + " local work id, " + localWorkId +
                    " tools id list " + updatedMobileAppToolIds);
        }
        boolean isWorking = (PendingIntent.getBroadcast(mContext, 1001, workIntent, PendingIntent.FLAG_NO_CREATE) != null);//just changed the flag
        if (isWorking) {
            alarmManager.cancel(workPendingIntent);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
                    10 * (minFrequency / 10), workPendingIntent);
        } else {
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
                    10 * (minFrequency / 10), workPendingIntent);
        }

The alarm manager receiver class then call workmanager class.然后警报管理器接收器类调用 workmanager 类。 Here is the work manager class这是工作经理类

    @SuppressLint("WrongThread")
@NonNull
@Override
public Result doWork() {
    // Code to be execute

} }

Any help on this Appreciated.对此表示赞赏的任何帮助。

You can achieve scheduled tasks using periodic work requests.您可以使用定期工作请求来完成计划任务。 Here's how you can do it:以下是您的操作方法:

  • Define constraints like if device should be connected or should have sufficient battery.定义约束条件,例如设备是否应该连接或是否应该有足够的电池。

  • Now create a periodic request with constraints & if required define a tag name.现在创建一个带有约束的定期请求,如果需要,定义一个标签名称。 Also set a delay this which will set a initial delay for your work to start.还要设置一个延迟,这将为您的工作开始设置一个初始延迟。

  • And create a enqueueUniquePeriodicWork by providing unique work name & the ExistingPeriodicWorkPolicy which you can read here ExistingPeriodicWorkPolicy Enum values并通过提供唯一的工作名称和 ExistingPeriodicWorkPolicy 创建一个 enqueueUniquePeriodicWork,您可以在此处阅读ExistingPeriodicWorkPolicy 枚举值

  • By default periodic work will take 15 minutes to execute the work request.默认情况下,定期工作将需要 15 分钟来执行工作请求。

     Constraints constraints = new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).setRequiresBatteryNotLow(true).build(); PeriodicWorkRequest periodicRequest = new PeriodicWorkRequest.Builder(MyWorkerClass.class, 15, TimeUnit.MINUTES) .addTag("myPeriodicRequest") .setConstraints(constraints) .setInitialDelay(5, TimeUnit.MINUTES) .build(); WorkManager.getInstance(context).enqueueUniquePeriodicWork("myPeriodicRequest", ExistingPeriodicWorkPolicy.APPEND_OR_REPLACE, periodicRequest);

You are missing the point on how the WorkManager should be used.您错过了应如何使用 WorkManager 的要点。 You can be exact with AalarmManager, but then you pass your execution to something inexact - the WorkManager.您可以使用 AalarmManager 精确,但随后您将执行传递给不精确的东西 - WorkManager。 So no point in using the AlarmManager at all.所以根本没有使用 AlarmManager 的意义。 Just use the WorkManager directly with either Periodic Request or maybe some Trigger.只需将 WorkManager 直接与定期请求或某些触发器一起使用。 You can check more here in my answer comparing the different APIs.您可以在我比较不同 API 的答案中查看更多信息。

Background processing in modern Android 现代Android中的后台处理

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

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