警报管理器触发通知

时间:2013-01-26 22:42:22

标签: android alarm notificationmanager

我有一个警报管理器,可以在每个星期天触发通知。它工作正常,但每当我打开应用程序时,即使它不是星期日,也会显示通知。应用程序打开时是否触发onReceive方法?如何更改它以便在打开应用程序时不显示通知?

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);
     }

 }

1 个答案:

答案 0 :(得分:2)

这与setRepeating()如果过去会立即触发警报的事实有关。您需要计算时间差,然后进行报警。添加到日历似乎是一种简单的方法。

例如,如果您使用的是日历,当天是星期一:

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.