Periodic work requests using WorkManager not working

Qasem Salehy picture Qasem Salehy · Jun 30, 2018 · Viewed 12.6k times · Source

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) !

here is my main activity :

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);
}

}

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;
}

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

Answer

ianhanniballake picture ianhanniballake · Jun 30, 2018

Per the PeriodicWorkRequest.Builder documentation:

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

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