简体   繁体   English

使用定期工作管理器每 30 秒生成一次通知

[英]generate a notification after every 30 sec using periodic work manager

I am developing an android app in which i want to generate notification after every 30 sec.我正在开发一个 android 应用程序,我想在每 30 秒后生成一次通知。 I could use simple timer function but i need to generate notification even app is completely destroyed.我可以使用简单的计时器功能,但即使应用程序被完全破坏,我也需要生成通知。 Now for that purpose i used work manager api.I never used this api but work manager work only for first time.现在为此目的,我使用了工作管理器 api。我从未使用过这个 api,但工作管理器只在第一次使用。 The notification doesn't come again通知不再来

this is my worker class这是我的工人阶级

@Override
public Result doWork() {

    displayNotification();

      return Result.SUCCESS;
   }

this function runs in oncreate of main activity此函数在主要活动的 oncreate 中运行

    PeriodicWorkRequest dailyWorkRequest = new PeriodicWorkRequest.Builder(MyWorker.class,30,TimeUnit.SECONDS)
            .setConstraints(Constraints.NONE).build();

    WorkManager.getInstance()
            .enqueue(dailyWorkRequest);

this is libraray i used for work manager这是我用于工作经理的图书馆

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

You want your app to essentially run all the time.您希望您的应用程序基本上一直运行。 In that case just use a foreground Service , and manage notifications that way.在这种情况下,只需使用前台Service ,并以这种方式管理通知。

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

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