Reputation: 169
I have requirement of pushing in app notification to user based on following logic.
I have used PeriodicWorkRequest
work manager as follows, it's working fine until the device is restarted.
Once device is restarted, my work is NOT getting triggered.
build.gradle ---
implementation 'android.arch.work:work-runtime:1.0.0-alpha04'
Java code
PeriodicWorkRequest showNotification =
new PeriodicWorkRequest.Builder(ShowNotificationWorkManager.class, interval,
TimeUnit.HOURS)
.addTag(notificationType)
.setInputData(myData)
.build();
getWorkManger().enqueue(showNotification);
Upvotes: 16
Views: 8913
Reputation: 21
Note: In some new versions of android, the boot complete intent is protected which means only the android system can either call or listen for it. Im currently experiencing this problem as I want to restart my service on boot complete. It throws a SecurityException .
Upvotes: 0
Reputation: 2184
I've tried this and works also for HTC phones.
<receiver android:name=".WorkManagerStartReceiver" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
Upvotes: 0
Reputation: 26821
You called getWorkManger().enqueue(showNotification)
. Instead, you should enqueue periodic work because yours is a Periodic Operation:
workManager = WorkManager.getInstance(this)
workManager.enqueueUniquePeriodicWork(
WORKER_NAME, ExistingPeriodicWorkPolicy.KEEP, workRequest
)
Upvotes: 1
Reputation: 479
I've tried this code & worked with me on old APIs and new ones: This code which executes the service (workmanager) every 15 min when the device reboots. for AndroidManifest.xml file:
<receiver android:name=".WorkManagerStartReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
And on WorkManagerStartReceiver class:
public class WorkManagerStartReceiver extends BroadcastReceiver {
WorkManager mWorkManager;
@Override
public void onReceive(Context context, Intent intent) {
PeriodicWorkRequest.Builder myWorkBuilder =
new PeriodicWorkRequest.Builder(TestWorker.class,
PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS,
TimeUnit.MILLISECONDS);
PeriodicWorkRequest myWork = myWorkBuilder.build();
mWorkManager = WorkManager.getInstance(context);
mWorkManager.enqueue(myWork);
}
}
While TestWorker.class is the class that extends Worker:
public class TestWorker extends Worker {
public TestWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
}
@NonNull
@Override
public Result doWork() {
//your work that you want to execute here
return null;
}
}
And this snippet (on MainActivity) that opens when the app opens if you want the service to start working when the app opens.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PeriodicWorkRequest.Builder myWorkBuilder =
new PeriodicWorkRequest.Builder(TestWorker.class,
PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS,
TimeUnit.MILLISECONDS);
PeriodicWorkRequest myWork = myWorkBuilder.build();
WorkManager.getInstance(MainActivity.this)
.enqueueUniquePeriodicWork("testworker", ExistingPeriodicWorkPolicy.KEEP, myWork);
}
Upvotes: 9
Reputation: 1170
if PeriodicWorkRequest is not working after device is rebooting, you can fix this issue by doing these steps.
Simply define a BroadcastReceiver and schedule your WorKManager in onReceive(-) method.
public class WorkManagerStartReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Constraints constraints = new Constraints.Builder()
.setRequiresCharging(false)
.build();
PeriodicWorkRequest saveRequest =
new PeriodicWorkRequest.Builder(ToastWorker.class, 15, TimeUnit.MINUTES)
.setConstraints(constraints)
.build();
WorkManager.getInstance(context)
.enqueue(saveRequest);
} }
Now define your BroadcastReceiver in manifest.xml file
<application
----
>
<activity android:name=".MainActivity"/>
<receiver android:name=".WorkManagerStartReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
Hey don't forget to add boot permission in your manifest file android.permission.RECEIVE_BOOT_COMPLETED
Upvotes: -1
Reputation: 449
Periodic work request can scheduled once when app is started first time on the device. We can use shared preference to identify whether the app is started first time or not. Moreover, in order to repeat the work after boot, we can start the work in a BroadcastReceiver which will be triggered after rebooting device.
Upvotes: -2
Reputation: 2332
Please add the following permission in your android manifest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Upvotes: -1