I have a scenario in which an alarm will set at time using time picker. I want to repeat alarm say 2 hours, 4 hours and 6 hours. I have done this scenario. But I want to start this repeating alarm everyday.
Here is my code.
public void saveButtonClick(View view){
name = drugName.getText().toString();
doseQuantity = doseNumber.getText().toString();
createPendingIntent(name, doseQuantity);
if (checkBox.isChecked()){
setRepeatingReminders();
} else {
createReminders();
}
Toast.makeText(getApplicationContext(), "You have successfully added", Toast.LENGTH_SHORT).show();
}
private void createPendingIntent(String drugName, String doseQuantity){
Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;
intent = new Intent(MainActivity.this, NotificationReceiver.class);
intent.putExtra("Id", m);
intent.putExtra("Drug Name", drugName);
intent.putExtra("Dose", doseQuantity);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, m, intent, 0);
}
private void createReminders(){
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timeInMillisecond, AlarmManager.INTERVAL_DAY, pendingIntent);
}
private void setRepeatingReminders(){
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
long twoHours = 2 * 60 * 60 * 1000L;
long fourHours = 4 * 60 * 60 * 1000L;
long sixHours = 6 * 60 * 60 * 1000L;
if (spinnerValue.equals("2 hours")){
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, timeInMillisecond, twoHours, pendingIntent);
} else if (spinnerValue.equals("2min")){
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timeInMillisecond, fourHours, pendingIntent);
} else if (spinnerValue.equals("3min")){
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timeInMillisecond, sixHours, pendingIntent);
}
}
and this is receiver class that I have used
public class NotificationReceiver extends BroadcastReceiver {
private Intent intent1;
private PendingIntent pendingIntent;
private int notificationId;
private String drugName, dose;
@Override
public void onReceive(Context context, Intent intent) {
intent1 = new Intent(context, MainActivity.class);
Log.v("Id", String.valueOf(notificationId));
drugName = intent.getStringExtra("Drug Name");
dose = intent.getStringExtra("Dose");
notificationId = intent.getIntExtra("Id", -1);
pendingIntent = PendingIntent.getActivity(context, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
// Build the notification
Notification.Builder notificationBuilder = new Notification.Builder(context)
.setContentText("Hey, It's time to take your " + drugName + " Dose " + dose)
.setContentTitle("Take Your Pill")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Pass the notification to the notification manager
mNotificationManager.notify(notificationId, notificationBuilder.build());
}
}
Please somebody help me.