Reputation: 972
I have an alarm manager that triggers a notification every Sunday. It works fine but whenever I open the application, the notification is displayed even if it is not Sunday. Is the onReceive method triggered when the application opens? How could I change it so the notification does not display when the app is opened?
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's 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);
}
}
Upvotes: 3
Views: 1976
Reputation: 36449
It has to do with the fact that setRepeating() will trigger the alarm immediately if it is in the past. You will need to compute the time difference then shedule the alarm. Adding to the calendar seems like an easy way to do it.
Eg, 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.
Upvotes: 2