使用 WorkManager 的定期工作请求不起作用 [英] Periodic work requests using WorkManager not working

查看:43
本文介绍了使用 WorkManager 的定期工作请求不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个定期工作管理器脚本,但它只是在我打开应用程序时运行,并且它只运行一次(非定期)!

i am trying to write a periodic workmanager script but it just run when i open the app and it just run one time (not periodic) !

这是我的主要活动:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_work);

    Intent intent = new Intent();
    PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0);
    NotifyWorker.pendingIntent = pendingIntent;
    NotifyWorker.context = this;

    PeriodicWorkRequest periodicWorkRequest = new PeriodicWorkRequest.Builder(NotifyWorker.class, 1, TimeUnit.MINUTES).build();
    WorkManager.getInstance().enqueue(periodicWorkRequest);
}

}

这是我的dowork方法:

and this is my dowork method :

public Result doWork() {
    Log.i("wd","wd");

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context,"ctx")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.mipmap.ic_launcher))
            .setSmallIcon(R.drawable.logo)
            .setContentTitle("Title")
            .setContentText("Desc")
            .setContentIntent(pendingIntent);

    android.app.NotificationManager notificationManager =
            (android.app.NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 , notificationBuilder.build());

    return Result.SUCCESS;
}

为什么不是每 1 分钟运行一次?我想念什么?

why its not run every 1 minute ? what i miss ?

推荐答案

根据 PeriodicWorkRequest.Builder 文档:

intervalMillis 必须大于或等于 PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS

The intervalMillis must be greater than or equal to PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS

该值当前设置为 900000 - 即 15 分钟.

That value is currently set to 900000 - i.e, 15 minutes.

这篇关于使用 WorkManager 的定期工作请求不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆