毎週日曜日に通知をトリガーするアラームマネージャーがあります。正常に動作しますが、アプリケーションを開くと、日曜日でなくても通知が表示されます。アプリケーションが開いたときにonReceiveメソッドがトリガーされますか?アプリを開いたときに通知が表示されないように変更するにはどうすればよいですか?
コード:
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);
}
}