在android oreo中重启设备后,PeriodicWorkRequest无法正常工作

问题描述 投票:6回答:2

我要求根据以下逻辑将app通知推送给用户。

  • 每24小时后会显示A类通知。
  • 每7天后会显示B类通知。
  • 每15天后会显示C类通知。

我使用PeriodicWorkRequest工作管理器如下,它工作正常,直到设备重新启动。设备重启后,我的工作没有触发。

build.gradle ---

implementation 'android.arch.work:work-runtime:1.0.0-alpha04'

Java代码

PeriodicWorkRequest showNotification =
                new PeriodicWorkRequest.Builder(ShowNotificationWorkManager.class, interval,
                        TimeUnit.HOURS)
                        .addTag(notificationType)
                        .setInputData(myData)
                        .build();

getWorkManger().enqueue(showNotification);
android android-workmanager
2个回答
0
投票

在设备完成启动时触发的BroadcastReceiver中重新启动PeriodicWorkRequest。像意图过滤器一样。

<receiver
        android:name=".warrantyregistration.boot.BootReceiver"
        android:enabled="@bool/configuration_for_receiver_service">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

this回答了重启设备时OneTimeWorkRequest的行为方式。


0
投票

请在您的Android清单中添加以下权限

   <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
© www.soinside.com 2019 - 2024. All rights reserved.