Issue with AlarmReceiver Android

70 Views Asked by At

Introduction: Through a ToggleButton I make my user to enable or disable the alarm reminder. The alarm remember my user to record their activity one hour after the last time he has entered the app also do not want the alarm to sound at night. This all works fine.

 public void each_hour_alarm() {
    Calendar cal = Calendar.getInstance(); 
    cal.setTime(new Date());               
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    if(hour <= 20 && hour >= 7) {             
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
    notificationIntent.addCategory("android.intent.category.DEFAULT");

    PendingIntent broadcast = getBroadcast(getApplicationContext(), 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR, 1);
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), broadcast);
        stop_alarm_9am();
    }else {
        stop_each_hour_alarm();
        alarm_9am();  
    }
}

The problem begins when I call a new method: alarm_9am(); The new method raises an alarm at 9 am if the user has not entered before the app. This is where I have the problem. The alarm sounds at 9am, but also, and this is my problem, it also sounds every time I log activity between 21h and 23: 59h. I do not understand why this happens. Any ideas?? This is de code:

 public void alarm_9am() {

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent notificationIntent = new Intent("android.media.buenosdias.action.DISPLAY_NOTIFICATION");
        notificationIntent.addCategory("android.intent.buenosdias.category.DEFAULT");

        PendingIntent broadcast = getBroadcast(getApplicationContext(), 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 9);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 1);
        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), broadcast);}

Code to stop alarm_9am();

 public void stop_alarm_9am() {
    Intent intentlocal = new Intent("android.media.buenosdias.action.DISPLAY_NOTIFICATION");
    intentlocal.addCategory("android.intent.buenosdias.category.DEFAULT");
    PendingIntent pilocal = getBroadcast(getApplicationContext(), 100, intentlocal, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.cancel(pilocal);
    pilocal.cancel();
}

And my AlarmReceiver;

public class AlarmReceiver_buenos_dias extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
    Intent notificationIntent = new Intent(context, v_agua.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(v_agua.class);
    stackBuilder.addNextIntent(notificationIntent);
    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    Notification notification = builder.setContentTitle("Good Morning")
            .setContentText("Begin New Day")
            .setTicker("Go to app") 

            .setSmallIcon(R.drawable.logo_launcher_a)
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(""))
            .setAutoCancel(true)
            .setContentIntent(pendingIntent).build();

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notification);
}}

My issue is that the alarm sounds between 21h to 23:59 every time I enter in activity and i dont know why. I need not sound the alarm every time I log in activity between 21h and 23:59, I just want alarm_9am(); sounds at 9am Thanks!

0

There are 0 best solutions below