Alarm Manager Trigger Notification

I have an alarm manager that fires a notification every Sunday. It works fine, but whenever I open the app, a notification is shown even if it's not Sunday. Does the onReceive method get thrown when the app is opened? How can I change it so that the notification is not shown when I open the app?

CODE:

package com.sjjgames.abortionappnoads;

import java.util.Calendar;
import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.widget.Toast;

    public class Alarm extends BroadcastReceiver
    {
         @Override
         public void onReceive(Context context, Intent intent) 
         {   
             PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
             PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
             wl.acquire();

                // Put here YOUR code.
                //NOTIFICATION
                NotificationCompat.Builder mBuilder =
                        new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.icon)
                        .setContentTitle("Fetal Development")
                        .setContentText("Click here to view you child development for this week!");
                Intent resultIntent = new Intent(context, StatusOfChild.class);
                TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
                stackBuilder.addParentStack(StatusOfChild.class);
                stackBuilder.addNextIntent(resultIntent);
                PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
                mBuilder.setContentIntent(resultPendingIntent);
                NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                mNotificationManager.notify(9011, mBuilder.build());

             wl.release();
         }

     public void SetAlarm(Context context)
     {
         AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
         Intent i = new Intent(context, Alarm.class);
         PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
         /*
         Calendar calendar = Calendar.getInstance();
         calendar.set(Calendar.DAY_OF_WEEK, 1);
         calendar.set(Calendar.HOUR_OF_DAY, 12);
         calendar.set(Calendar.MINUTE, 00);
         calendar.set(Calendar.SECOND, 00);
         */
        long day = 0;
        Calendar dayCalendar = Calendar.getInstance();
        long dayOfWeek = dayCalendar.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == 1){ //Sunday
            day = System.currentTimeMillis();
        }
        if (dayOfWeek == 2){ //Monday
            day = 24*60*60*1000*2;
        }
        if (dayOfWeek == 3){ //Tuesday
            day = 24*60*60*1000*3;
        }
        if (dayOfWeek == 4){ //Wednesday
            day = 24*60*60*1000*4;
        }
        if (dayOfWeek == 5){ //Thursday
            day = 24*60*60*1000*5;
        }
        if (dayOfWeek == 6){ //Friday
            day = 24*60*60*1000*6;
        }
        if (dayOfWeek == 7){ //Saturday
            day = 24*60*60*1000;
        }
        am.setRepeating(AlarmManager.RTC_WAKEUP, day, 24*60*60*1000*7, pi);

     }

     public void CancelAlarm(Context context)
     {
         Intent intent = new Intent(context, Alarm.class);
         PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
         AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
         alarmManager.cancel(sender);
     }

 }

      

+3


source to share


1 answer


This is because setRepeating () will immediately raise an alarm if it was in the past. You will need to calculate the time difference and then deploy the alarm. Adding to the calendar seems like an easy way to do it.

For example, if you were using Calendar and the current day is Monday:



Calendar calendar = Calendar.getInstance();
         calendar.set(Calendar.HOUR_OF_DAY, 12);
         calendar.set(Calendar.MINUTE, 00);
         calendar.set(Calendar.SECOND, 00);
         calendar.add (Calendar.DATE, 6); //add 6 days.

      

+2


source







All Articles