Pol Hallen
Pol Hallen

Reputation: 1872

random time (to notification)

I need to generate a random date (hour+minutes) to fire a random notification. What is the best way? Build 2 random numbers (from 0 to 23 for hours and from 0 to 59 for minutes) and put they to a variable?

Random generator = new Random();
int b = 23
int random = generator.nextInt(b);
boolean flag = generator.nextBoolean();
x=random;

Start notification:

Calendar cal1 = Calendar.getInstance();
cal1.set(Calendar.HOUR_OF_DAY, 05);
cal1.set(Calendar.MINUTE, 00);

Intent intent2 = new Intent(context, Random.class);
PendingIntent random = PendingIntent.getBroadcast(context, 0, intent2, 0);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal1.getTimeInMillis(),  DateUtils.DAY_IN_MILLIS, random);

Upvotes: 0

Views: 393

Answers (1)

DeepakAndroid
DeepakAndroid

Reputation: 137

yup....try to generate two random numbers,say 0-23 hrs and another random number 0-59...then u can combine these two random numbers into a single variable for representing the data and time

Upvotes: 1

Related Questions