frenzied_hound
frenzied_hound

Reputation: 3

Executing a method only once a day android java

public ListenableFuture<?> fakeUpload() {
    ImmutableList.Builder<Keys> builder = ImmutableList.builder();
    for (int i = 0; i < 14; i++) {
      byte[] bytes = new byte[KEY_SIZE_BYTES];
      RAND.nextBytes(bytes);
      builder.add(
          Keys.newBuilder()
              .setKeyBytes(bytes)
              .setIntervalNumber(FAKE_INTERVAL_NUM)
              .build());

    }

    return doUpload(builder.build(), true);
  }

I want to make these keys upload only once a day. How can i do that?

Upvotes: 0

Views: 132

Answers (1)

Aditya Tyagi
Aditya Tyagi

Reputation: 431

Now you can write this code in application activity where you want to or create a service and write the code there :-

private fun shouldUploadFakeKeys(): Boolean {
    if (sharedPreferenceUtil.getInt(Constants.EXTRA_DAILY_KEYS_UPLOAD) != getTodaysDate()) {
        sharedPreferenceUtil.putBoolean(Contants.EXTRA_HAS_FAKE_KEYS_UPLOADED_TODAY,false)
        return true
    } else if (sharedPreferenceUtil.getBoolean(Contants.EXTRA_HAS_FAKE_KEYS_UPLOADED_TODAY)) {
        return false
    }
    return true
}
fun getTodaysDate(): Int {
        return ZonedDateTime.now().dayOfMonth
    }

Create a constants file if you don't have one already:-

Constants.kt

class Constants{
    companionObject{
         const val EXTRA_DAILY_KEYS_UPLOAD = "EXTRA_DAILY_KEYS_UPLOAD"
         const val EXTRA_HAS_FAKE_KEYS_UPLOADED_TODAY = "EXTRA_HAS_FAKE_KEYS_UPLOADED_TODAY"
    }
}

Upvotes: 3

Related Questions