Periodic daily work requests using WorkManager












7















How to properly use the new WorkManager from Android Jetpack to schedule a one per day periodic work that should do some action on a daily basis and exactly one time?



The idea was to check if the work with a given tag already exists using WorkManager and to start a new periodic work otherwise.



I've tried to do it using next approach:



public static final String CALL_INFO_WORKER = "Call worker";

...

WorkManager workManager = WorkManager.getInstance();
List<WorkStatus> value = workManager.getStatusesByTag(CALL_INFO_WORKER).getValue();
if (value == null) {
WorkRequest callDataRequest = new PeriodicWorkRequest.Builder(CallInfoWorker.class,
24, TimeUnit.HOURS, 3, TimeUnit.HOURS)
.addTag(CALL_INFO_WORKER)
.build();
workManager.enqueue(callDataRequest);
}


But the value is always null, even if I put a breakpoint inside the Worker's doWork() method (so it is definitely in progress) and check the work status from another thread.










share|improve this question





























    7















    How to properly use the new WorkManager from Android Jetpack to schedule a one per day periodic work that should do some action on a daily basis and exactly one time?



    The idea was to check if the work with a given tag already exists using WorkManager and to start a new periodic work otherwise.



    I've tried to do it using next approach:



    public static final String CALL_INFO_WORKER = "Call worker";

    ...

    WorkManager workManager = WorkManager.getInstance();
    List<WorkStatus> value = workManager.getStatusesByTag(CALL_INFO_WORKER).getValue();
    if (value == null) {
    WorkRequest callDataRequest = new PeriodicWorkRequest.Builder(CallInfoWorker.class,
    24, TimeUnit.HOURS, 3, TimeUnit.HOURS)
    .addTag(CALL_INFO_WORKER)
    .build();
    workManager.enqueue(callDataRequest);
    }


    But the value is always null, even if I put a breakpoint inside the Worker's doWork() method (so it is definitely in progress) and check the work status from another thread.










    share|improve this question



























      7












      7








      7


      1






      How to properly use the new WorkManager from Android Jetpack to schedule a one per day periodic work that should do some action on a daily basis and exactly one time?



      The idea was to check if the work with a given tag already exists using WorkManager and to start a new periodic work otherwise.



      I've tried to do it using next approach:



      public static final String CALL_INFO_WORKER = "Call worker";

      ...

      WorkManager workManager = WorkManager.getInstance();
      List<WorkStatus> value = workManager.getStatusesByTag(CALL_INFO_WORKER).getValue();
      if (value == null) {
      WorkRequest callDataRequest = new PeriodicWorkRequest.Builder(CallInfoWorker.class,
      24, TimeUnit.HOURS, 3, TimeUnit.HOURS)
      .addTag(CALL_INFO_WORKER)
      .build();
      workManager.enqueue(callDataRequest);
      }


      But the value is always null, even if I put a breakpoint inside the Worker's doWork() method (so it is definitely in progress) and check the work status from another thread.










      share|improve this question
















      How to properly use the new WorkManager from Android Jetpack to schedule a one per day periodic work that should do some action on a daily basis and exactly one time?



      The idea was to check if the work with a given tag already exists using WorkManager and to start a new periodic work otherwise.



      I've tried to do it using next approach:



      public static final String CALL_INFO_WORKER = "Call worker";

      ...

      WorkManager workManager = WorkManager.getInstance();
      List<WorkStatus> value = workManager.getStatusesByTag(CALL_INFO_WORKER).getValue();
      if (value == null) {
      WorkRequest callDataRequest = new PeriodicWorkRequest.Builder(CallInfoWorker.class,
      24, TimeUnit.HOURS, 3, TimeUnit.HOURS)
      .addTag(CALL_INFO_WORKER)
      .build();
      workManager.enqueue(callDataRequest);
      }


      But the value is always null, even if I put a breakpoint inside the Worker's doWork() method (so it is definitely in progress) and check the work status from another thread.







      android android-architecture-components android-jetpack android-workmanager






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 15 '18 at 19:28







      Gaket

















      asked May 15 '18 at 18:32









      GaketGaket

      2,57911944




      2,57911944
























          3 Answers
          3






          active

          oldest

          votes


















          11














          You can now use enqueueUniquePeriodicWork method. It was added in 1.0.0-alpha03 release of the WorkManager.






          share|improve this answer



















          • 2





            It's buggy.. it doesn't work for all api levels. event with alpha04..

            – atasoyh
            Jul 16 '18 at 9:19













          • @atasoyh do you have any bug report for that I can read into?

            – stefana
            Jul 18 '18 at 11:42











          • @stefana github.com/googlecodelabs/android-workmanager/issues/28

            – atasoyh
            Jul 18 '18 at 15:11






          • 1





            alpha05 has been released.

            – Lou Morda
            Jul 26 '18 at 22:56











          • alpha10 has been released.

            – Al-Amin
            Nov 7 '18 at 12:39



















          1














          Eventually, I understood, that the problem lies in the way how the LiveData is used. Because there are no observers, there is no value inside.



          The problem with using just the PeriodicWork is that it doesn't ensure the uniqueness of the work you want to do. In other words, it is possible to have many works that will be active simultaneously firing more times than you need.






          share|improve this answer


























          • I want show notification once per day i tried periodicwork.seems its not working. PeriodicWorkRequest.Builder myWorkBuilder = new PeriodicWorkRequest.Builder(MyWorker.class, 24, TimeUnit.HOURS); .It showing one day only after that its not showing and also if app is running background it will show correctly.I called workrequest in application class

            – Lavanya Velusamy
            May 16 '18 at 3:35













          • I haven't been able to check it in 24-hours length but my 15-minutes tests went well, even after the phone restart.

            – Gaket
            May 16 '18 at 4:41











          • Can you share your code for 15 mins...I tried this PeriodicWorkRequest.Builder myWorkBuilder = new PeriodicWorkRequest.Builder(MyWorker.class, 5, TimeUnit.SECONDS,5,TimeUnit.SECONDS); for test...To run every 5 seconds and also what is the first time and sec time interaval meaning?

            – Lavanya Velusamy
            May 16 '18 at 5:14











          • PeriodicWorkRequest.Builder myWorkBuilder = new PeriodicWorkRequest.Builder(MyWorker.class, 5, TimeUnit.SECONDS,5,TimeUnit.SECONDS); this code runs first 5 sec continuosly after that it didnt

            – Lavanya Velusamy
            May 16 '18 at 5:21











          • @LavanyaVelusamy Ideal Min time for periodic requests is 15 minutes (mentioned it in the IO 18).

            – Srikar Reddy
            May 16 '18 at 7:54





















          1














          You are looking for enqueueUniquePeriodicWork




          This method allows you to enqueue a uniquely-named
          PeriodicWorkRequest, where only one PeriodicWorkRequest of a
          particular name can be active at a time. For example, you may only
          want one sync operation to be active. If there is one pending, you can
          choose to let it run or replace it with your new work.




          Sample code



          public static final String TAG_MY_WORK = "mywork";

          public static void scheduleWork(String tag) {
          PeriodicWorkRequest.Builder photoCheckBuilder =
          new PeriodicWorkRequest.Builder(WorkManagerService.class, 1, TimeUnit.DAYS);
          PeriodicWorkRequest request = photoCheckBuilder.build();
          WorkManager.getInstance().enqueueUniquePeriodicWork(tag, ExistingPeriodicWorkPolicy.KEEP , request);
          }


          You get two types of ExistingPeriodicWorkPolicy




          KEEP



          If there is existing pending work with the same unique name, do
          nothing.



          REPLACE



          If there is existing pending work with the same unique name, cancel
          and delete it.







          share|improve this answer
























          • Thx, when the question was asked, that was not part of the API. And when it appeared, the resolved answer was given.

            – Gaket
            Nov 24 '18 at 10:36











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50357066%2fperiodic-daily-work-requests-using-workmanager%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          11














          You can now use enqueueUniquePeriodicWork method. It was added in 1.0.0-alpha03 release of the WorkManager.






          share|improve this answer



















          • 2





            It's buggy.. it doesn't work for all api levels. event with alpha04..

            – atasoyh
            Jul 16 '18 at 9:19













          • @atasoyh do you have any bug report for that I can read into?

            – stefana
            Jul 18 '18 at 11:42











          • @stefana github.com/googlecodelabs/android-workmanager/issues/28

            – atasoyh
            Jul 18 '18 at 15:11






          • 1





            alpha05 has been released.

            – Lou Morda
            Jul 26 '18 at 22:56











          • alpha10 has been released.

            – Al-Amin
            Nov 7 '18 at 12:39
















          11














          You can now use enqueueUniquePeriodicWork method. It was added in 1.0.0-alpha03 release of the WorkManager.






          share|improve this answer



















          • 2





            It's buggy.. it doesn't work for all api levels. event with alpha04..

            – atasoyh
            Jul 16 '18 at 9:19













          • @atasoyh do you have any bug report for that I can read into?

            – stefana
            Jul 18 '18 at 11:42











          • @stefana github.com/googlecodelabs/android-workmanager/issues/28

            – atasoyh
            Jul 18 '18 at 15:11






          • 1





            alpha05 has been released.

            – Lou Morda
            Jul 26 '18 at 22:56











          • alpha10 has been released.

            – Al-Amin
            Nov 7 '18 at 12:39














          11












          11








          11







          You can now use enqueueUniquePeriodicWork method. It was added in 1.0.0-alpha03 release of the WorkManager.






          share|improve this answer













          You can now use enqueueUniquePeriodicWork method. It was added in 1.0.0-alpha03 release of the WorkManager.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jun 26 '18 at 15:53









          Greg DanGreg Dan

          4,61132747




          4,61132747








          • 2





            It's buggy.. it doesn't work for all api levels. event with alpha04..

            – atasoyh
            Jul 16 '18 at 9:19













          • @atasoyh do you have any bug report for that I can read into?

            – stefana
            Jul 18 '18 at 11:42











          • @stefana github.com/googlecodelabs/android-workmanager/issues/28

            – atasoyh
            Jul 18 '18 at 15:11






          • 1





            alpha05 has been released.

            – Lou Morda
            Jul 26 '18 at 22:56











          • alpha10 has been released.

            – Al-Amin
            Nov 7 '18 at 12:39














          • 2





            It's buggy.. it doesn't work for all api levels. event with alpha04..

            – atasoyh
            Jul 16 '18 at 9:19













          • @atasoyh do you have any bug report for that I can read into?

            – stefana
            Jul 18 '18 at 11:42











          • @stefana github.com/googlecodelabs/android-workmanager/issues/28

            – atasoyh
            Jul 18 '18 at 15:11






          • 1





            alpha05 has been released.

            – Lou Morda
            Jul 26 '18 at 22:56











          • alpha10 has been released.

            – Al-Amin
            Nov 7 '18 at 12:39








          2




          2





          It's buggy.. it doesn't work for all api levels. event with alpha04..

          – atasoyh
          Jul 16 '18 at 9:19







          It's buggy.. it doesn't work for all api levels. event with alpha04..

          – atasoyh
          Jul 16 '18 at 9:19















          @atasoyh do you have any bug report for that I can read into?

          – stefana
          Jul 18 '18 at 11:42





          @atasoyh do you have any bug report for that I can read into?

          – stefana
          Jul 18 '18 at 11:42













          @stefana github.com/googlecodelabs/android-workmanager/issues/28

          – atasoyh
          Jul 18 '18 at 15:11





          @stefana github.com/googlecodelabs/android-workmanager/issues/28

          – atasoyh
          Jul 18 '18 at 15:11




          1




          1





          alpha05 has been released.

          – Lou Morda
          Jul 26 '18 at 22:56





          alpha05 has been released.

          – Lou Morda
          Jul 26 '18 at 22:56













          alpha10 has been released.

          – Al-Amin
          Nov 7 '18 at 12:39





          alpha10 has been released.

          – Al-Amin
          Nov 7 '18 at 12:39













          1














          Eventually, I understood, that the problem lies in the way how the LiveData is used. Because there are no observers, there is no value inside.



          The problem with using just the PeriodicWork is that it doesn't ensure the uniqueness of the work you want to do. In other words, it is possible to have many works that will be active simultaneously firing more times than you need.






          share|improve this answer


























          • I want show notification once per day i tried periodicwork.seems its not working. PeriodicWorkRequest.Builder myWorkBuilder = new PeriodicWorkRequest.Builder(MyWorker.class, 24, TimeUnit.HOURS); .It showing one day only after that its not showing and also if app is running background it will show correctly.I called workrequest in application class

            – Lavanya Velusamy
            May 16 '18 at 3:35













          • I haven't been able to check it in 24-hours length but my 15-minutes tests went well, even after the phone restart.

            – Gaket
            May 16 '18 at 4:41











          • Can you share your code for 15 mins...I tried this PeriodicWorkRequest.Builder myWorkBuilder = new PeriodicWorkRequest.Builder(MyWorker.class, 5, TimeUnit.SECONDS,5,TimeUnit.SECONDS); for test...To run every 5 seconds and also what is the first time and sec time interaval meaning?

            – Lavanya Velusamy
            May 16 '18 at 5:14











          • PeriodicWorkRequest.Builder myWorkBuilder = new PeriodicWorkRequest.Builder(MyWorker.class, 5, TimeUnit.SECONDS,5,TimeUnit.SECONDS); this code runs first 5 sec continuosly after that it didnt

            – Lavanya Velusamy
            May 16 '18 at 5:21











          • @LavanyaVelusamy Ideal Min time for periodic requests is 15 minutes (mentioned it in the IO 18).

            – Srikar Reddy
            May 16 '18 at 7:54


















          1














          Eventually, I understood, that the problem lies in the way how the LiveData is used. Because there are no observers, there is no value inside.



          The problem with using just the PeriodicWork is that it doesn't ensure the uniqueness of the work you want to do. In other words, it is possible to have many works that will be active simultaneously firing more times than you need.






          share|improve this answer


























          • I want show notification once per day i tried periodicwork.seems its not working. PeriodicWorkRequest.Builder myWorkBuilder = new PeriodicWorkRequest.Builder(MyWorker.class, 24, TimeUnit.HOURS); .It showing one day only after that its not showing and also if app is running background it will show correctly.I called workrequest in application class

            – Lavanya Velusamy
            May 16 '18 at 3:35













          • I haven't been able to check it in 24-hours length but my 15-minutes tests went well, even after the phone restart.

            – Gaket
            May 16 '18 at 4:41











          • Can you share your code for 15 mins...I tried this PeriodicWorkRequest.Builder myWorkBuilder = new PeriodicWorkRequest.Builder(MyWorker.class, 5, TimeUnit.SECONDS,5,TimeUnit.SECONDS); for test...To run every 5 seconds and also what is the first time and sec time interaval meaning?

            – Lavanya Velusamy
            May 16 '18 at 5:14











          • PeriodicWorkRequest.Builder myWorkBuilder = new PeriodicWorkRequest.Builder(MyWorker.class, 5, TimeUnit.SECONDS,5,TimeUnit.SECONDS); this code runs first 5 sec continuosly after that it didnt

            – Lavanya Velusamy
            May 16 '18 at 5:21











          • @LavanyaVelusamy Ideal Min time for periodic requests is 15 minutes (mentioned it in the IO 18).

            – Srikar Reddy
            May 16 '18 at 7:54
















          1












          1








          1







          Eventually, I understood, that the problem lies in the way how the LiveData is used. Because there are no observers, there is no value inside.



          The problem with using just the PeriodicWork is that it doesn't ensure the uniqueness of the work you want to do. In other words, it is possible to have many works that will be active simultaneously firing more times than you need.






          share|improve this answer















          Eventually, I understood, that the problem lies in the way how the LiveData is used. Because there are no observers, there is no value inside.



          The problem with using just the PeriodicWork is that it doesn't ensure the uniqueness of the work you want to do. In other words, it is possible to have many works that will be active simultaneously firing more times than you need.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 15 '18 at 19:26

























          answered May 15 '18 at 19:16









          GaketGaket

          2,57911944




          2,57911944













          • I want show notification once per day i tried periodicwork.seems its not working. PeriodicWorkRequest.Builder myWorkBuilder = new PeriodicWorkRequest.Builder(MyWorker.class, 24, TimeUnit.HOURS); .It showing one day only after that its not showing and also if app is running background it will show correctly.I called workrequest in application class

            – Lavanya Velusamy
            May 16 '18 at 3:35













          • I haven't been able to check it in 24-hours length but my 15-minutes tests went well, even after the phone restart.

            – Gaket
            May 16 '18 at 4:41











          • Can you share your code for 15 mins...I tried this PeriodicWorkRequest.Builder myWorkBuilder = new PeriodicWorkRequest.Builder(MyWorker.class, 5, TimeUnit.SECONDS,5,TimeUnit.SECONDS); for test...To run every 5 seconds and also what is the first time and sec time interaval meaning?

            – Lavanya Velusamy
            May 16 '18 at 5:14











          • PeriodicWorkRequest.Builder myWorkBuilder = new PeriodicWorkRequest.Builder(MyWorker.class, 5, TimeUnit.SECONDS,5,TimeUnit.SECONDS); this code runs first 5 sec continuosly after that it didnt

            – Lavanya Velusamy
            May 16 '18 at 5:21











          • @LavanyaVelusamy Ideal Min time for periodic requests is 15 minutes (mentioned it in the IO 18).

            – Srikar Reddy
            May 16 '18 at 7:54





















          • I want show notification once per day i tried periodicwork.seems its not working. PeriodicWorkRequest.Builder myWorkBuilder = new PeriodicWorkRequest.Builder(MyWorker.class, 24, TimeUnit.HOURS); .It showing one day only after that its not showing and also if app is running background it will show correctly.I called workrequest in application class

            – Lavanya Velusamy
            May 16 '18 at 3:35













          • I haven't been able to check it in 24-hours length but my 15-minutes tests went well, even after the phone restart.

            – Gaket
            May 16 '18 at 4:41











          • Can you share your code for 15 mins...I tried this PeriodicWorkRequest.Builder myWorkBuilder = new PeriodicWorkRequest.Builder(MyWorker.class, 5, TimeUnit.SECONDS,5,TimeUnit.SECONDS); for test...To run every 5 seconds and also what is the first time and sec time interaval meaning?

            – Lavanya Velusamy
            May 16 '18 at 5:14











          • PeriodicWorkRequest.Builder myWorkBuilder = new PeriodicWorkRequest.Builder(MyWorker.class, 5, TimeUnit.SECONDS,5,TimeUnit.SECONDS); this code runs first 5 sec continuosly after that it didnt

            – Lavanya Velusamy
            May 16 '18 at 5:21











          • @LavanyaVelusamy Ideal Min time for periodic requests is 15 minutes (mentioned it in the IO 18).

            – Srikar Reddy
            May 16 '18 at 7:54



















          I want show notification once per day i tried periodicwork.seems its not working. PeriodicWorkRequest.Builder myWorkBuilder = new PeriodicWorkRequest.Builder(MyWorker.class, 24, TimeUnit.HOURS); .It showing one day only after that its not showing and also if app is running background it will show correctly.I called workrequest in application class

          – Lavanya Velusamy
          May 16 '18 at 3:35







          I want show notification once per day i tried periodicwork.seems its not working. PeriodicWorkRequest.Builder myWorkBuilder = new PeriodicWorkRequest.Builder(MyWorker.class, 24, TimeUnit.HOURS); .It showing one day only after that its not showing and also if app is running background it will show correctly.I called workrequest in application class

          – Lavanya Velusamy
          May 16 '18 at 3:35















          I haven't been able to check it in 24-hours length but my 15-minutes tests went well, even after the phone restart.

          – Gaket
          May 16 '18 at 4:41





          I haven't been able to check it in 24-hours length but my 15-minutes tests went well, even after the phone restart.

          – Gaket
          May 16 '18 at 4:41













          Can you share your code for 15 mins...I tried this PeriodicWorkRequest.Builder myWorkBuilder = new PeriodicWorkRequest.Builder(MyWorker.class, 5, TimeUnit.SECONDS,5,TimeUnit.SECONDS); for test...To run every 5 seconds and also what is the first time and sec time interaval meaning?

          – Lavanya Velusamy
          May 16 '18 at 5:14





          Can you share your code for 15 mins...I tried this PeriodicWorkRequest.Builder myWorkBuilder = new PeriodicWorkRequest.Builder(MyWorker.class, 5, TimeUnit.SECONDS,5,TimeUnit.SECONDS); for test...To run every 5 seconds and also what is the first time and sec time interaval meaning?

          – Lavanya Velusamy
          May 16 '18 at 5:14













          PeriodicWorkRequest.Builder myWorkBuilder = new PeriodicWorkRequest.Builder(MyWorker.class, 5, TimeUnit.SECONDS,5,TimeUnit.SECONDS); this code runs first 5 sec continuosly after that it didnt

          – Lavanya Velusamy
          May 16 '18 at 5:21





          PeriodicWorkRequest.Builder myWorkBuilder = new PeriodicWorkRequest.Builder(MyWorker.class, 5, TimeUnit.SECONDS,5,TimeUnit.SECONDS); this code runs first 5 sec continuosly after that it didnt

          – Lavanya Velusamy
          May 16 '18 at 5:21













          @LavanyaVelusamy Ideal Min time for periodic requests is 15 minutes (mentioned it in the IO 18).

          – Srikar Reddy
          May 16 '18 at 7:54







          @LavanyaVelusamy Ideal Min time for periodic requests is 15 minutes (mentioned it in the IO 18).

          – Srikar Reddy
          May 16 '18 at 7:54













          1














          You are looking for enqueueUniquePeriodicWork




          This method allows you to enqueue a uniquely-named
          PeriodicWorkRequest, where only one PeriodicWorkRequest of a
          particular name can be active at a time. For example, you may only
          want one sync operation to be active. If there is one pending, you can
          choose to let it run or replace it with your new work.




          Sample code



          public static final String TAG_MY_WORK = "mywork";

          public static void scheduleWork(String tag) {
          PeriodicWorkRequest.Builder photoCheckBuilder =
          new PeriodicWorkRequest.Builder(WorkManagerService.class, 1, TimeUnit.DAYS);
          PeriodicWorkRequest request = photoCheckBuilder.build();
          WorkManager.getInstance().enqueueUniquePeriodicWork(tag, ExistingPeriodicWorkPolicy.KEEP , request);
          }


          You get two types of ExistingPeriodicWorkPolicy




          KEEP



          If there is existing pending work with the same unique name, do
          nothing.



          REPLACE



          If there is existing pending work with the same unique name, cancel
          and delete it.







          share|improve this answer
























          • Thx, when the question was asked, that was not part of the API. And when it appeared, the resolved answer was given.

            – Gaket
            Nov 24 '18 at 10:36
















          1














          You are looking for enqueueUniquePeriodicWork




          This method allows you to enqueue a uniquely-named
          PeriodicWorkRequest, where only one PeriodicWorkRequest of a
          particular name can be active at a time. For example, you may only
          want one sync operation to be active. If there is one pending, you can
          choose to let it run or replace it with your new work.




          Sample code



          public static final String TAG_MY_WORK = "mywork";

          public static void scheduleWork(String tag) {
          PeriodicWorkRequest.Builder photoCheckBuilder =
          new PeriodicWorkRequest.Builder(WorkManagerService.class, 1, TimeUnit.DAYS);
          PeriodicWorkRequest request = photoCheckBuilder.build();
          WorkManager.getInstance().enqueueUniquePeriodicWork(tag, ExistingPeriodicWorkPolicy.KEEP , request);
          }


          You get two types of ExistingPeriodicWorkPolicy




          KEEP



          If there is existing pending work with the same unique name, do
          nothing.



          REPLACE



          If there is existing pending work with the same unique name, cancel
          and delete it.







          share|improve this answer
























          • Thx, when the question was asked, that was not part of the API. And when it appeared, the resolved answer was given.

            – Gaket
            Nov 24 '18 at 10:36














          1












          1








          1







          You are looking for enqueueUniquePeriodicWork




          This method allows you to enqueue a uniquely-named
          PeriodicWorkRequest, where only one PeriodicWorkRequest of a
          particular name can be active at a time. For example, you may only
          want one sync operation to be active. If there is one pending, you can
          choose to let it run or replace it with your new work.




          Sample code



          public static final String TAG_MY_WORK = "mywork";

          public static void scheduleWork(String tag) {
          PeriodicWorkRequest.Builder photoCheckBuilder =
          new PeriodicWorkRequest.Builder(WorkManagerService.class, 1, TimeUnit.DAYS);
          PeriodicWorkRequest request = photoCheckBuilder.build();
          WorkManager.getInstance().enqueueUniquePeriodicWork(tag, ExistingPeriodicWorkPolicy.KEEP , request);
          }


          You get two types of ExistingPeriodicWorkPolicy




          KEEP



          If there is existing pending work with the same unique name, do
          nothing.



          REPLACE



          If there is existing pending work with the same unique name, cancel
          and delete it.







          share|improve this answer













          You are looking for enqueueUniquePeriodicWork




          This method allows you to enqueue a uniquely-named
          PeriodicWorkRequest, where only one PeriodicWorkRequest of a
          particular name can be active at a time. For example, you may only
          want one sync operation to be active. If there is one pending, you can
          choose to let it run or replace it with your new work.




          Sample code



          public static final String TAG_MY_WORK = "mywork";

          public static void scheduleWork(String tag) {
          PeriodicWorkRequest.Builder photoCheckBuilder =
          new PeriodicWorkRequest.Builder(WorkManagerService.class, 1, TimeUnit.DAYS);
          PeriodicWorkRequest request = photoCheckBuilder.build();
          WorkManager.getInstance().enqueueUniquePeriodicWork(tag, ExistingPeriodicWorkPolicy.KEEP , request);
          }


          You get two types of ExistingPeriodicWorkPolicy




          KEEP



          If there is existing pending work with the same unique name, do
          nothing.



          REPLACE



          If there is existing pending work with the same unique name, cancel
          and delete it.








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 19:54









          KhemrajKhemraj

          13.4k33877




          13.4k33877













          • Thx, when the question was asked, that was not part of the API. And when it appeared, the resolved answer was given.

            – Gaket
            Nov 24 '18 at 10:36



















          • Thx, when the question was asked, that was not part of the API. And when it appeared, the resolved answer was given.

            – Gaket
            Nov 24 '18 at 10:36

















          Thx, when the question was asked, that was not part of the API. And when it appeared, the resolved answer was given.

          – Gaket
          Nov 24 '18 at 10:36





          Thx, when the question was asked, that was not part of the API. And when it appeared, the resolved answer was given.

          – Gaket
          Nov 24 '18 at 10:36


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50357066%2fperiodic-daily-work-requests-using-workmanager%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin