java - 如何使用通知播放多个闹钟?

标签 java android alarm repeat

我开发了一个使用通知的警报项目。我使用通知 id 作为唯一,并每分钟设置一次闹钟,即下午 2:45、2:46、2:47,但闹钟仅在最后一次触发,即下午 2:47。

为什么?

 int dayofweek;
 int notify;
 int notifyid=0;
 Cursor notifyvalue=db.getnotifyid();
 if (notifyvalue.moveToPosition(0))
 notifyid =notifyvalue.getInt(notifyvalue.getColumnIndex("notifyvalue"));

 if(notifyid==0)
 {
  db.InsertNotifyId(2600);
 }


                                Calendar calNow = Calendar.getInstance();
                                Calendar calSet = (Calendar) calNow.clone();

                                calSet.set(Calendar.HOUR_OF_DAY,22);
                                calSet.set(Calendar.MINUTE, 10);
                                calSet.set(Calendar.SECOND, 0);
                                calSet.set(Calendar.MILLISECOND, 0);

                            int notify1 = 0;
                            if(repeateddayname.equalsIgnoreCase("Everyday"))
                            {

                                   Cursor notifyvalue1=db.getnotifyid();
                                   if (notifyvalue1.moveToPosition(0))
                                      notify1=notifyvalue1.getInt(notifyvalue1.getColumnIndex("notifyvalue"));
                                   setAlarm(calSet, true,notify1+1,SplitAlramVoice[1]);
                                   db.InsertNotifyId(notify1+1);
                            }

                            else
                            {
                                String splitrepeateddayname[]=repeateddayname.split(",");

                                for(int i=0; i < splitrepeateddayname.length; i++)
                                {
                                    if(splitrepeateddayname[i].equalsIgnoreCase("Sunday"))
                                    {
                                        dayofweek=1+(7-calSet.DAY_OF_WEEK);
                                        Cursor notifyvalue1=db.getnotifyid();
                                         if (notifyvalue1.moveToPosition(0))
                                        notify1=notifyvalue1.getInt(notifyvalue1.getColumnIndex("notifyvalue"));
                                        setAlarm(calSet, false,notify1+1,SplitAlramVoice[1]);
                                         db.InsertNotifyId(notify1+1);
                                    }
                                    else if(splitrepeateddayname[i].equalsIgnoreCase("Monday"))
                                    {
                                        dayofweek=2+(7-calSet.DAY_OF_WEEK);
                                        Cursor notifyvalue1=db.getnotifyid();
                                        if (notifyvalue1.moveToPosition(0))
                                        notify1=notifyvalue1.getInt(notifyvalue1.getColumnIndex("notifyvalue"));
                                        setAlarm(calSet, false,notify1+1,SplitAlramVoice[1]);
                                         db.InsertNotifyId(notify1+1);
                                    }
                                    else if(splitrepeateddayname[i].equalsIgnoreCase("Tuesday"))
                                    {
                                        dayofweek=3+(7-calSet.DAY_OF_WEEK);
                                        Cursor notifyvalue1=db.getnotifyid();
                                        if (notifyvalue1.moveToPosition(0))
                                        notify1=notifyvalue1.getInt(notifyvalue1.getColumnIndex("notifyvalue"));
                                        setAlarm(calSet, false,notify1+1,SplitAlramVoice[1]);
                                         db.InsertNotifyId(notify1+1);
                                    }
                                    else if(splitrepeateddayname[i].equalsIgnoreCase("Wednesday"))
                                    {
                                        dayofweek=4+(7-calSet.DAY_OF_WEEK);
                                        Cursor notifyvalue1=db.getnotifyid();
                                        if (notifyvalue1.moveToPosition(0))
                                        notify1=notifyvalue1.getInt(notifyvalue1.getColumnIndex("notifyvalue"));
                                        setAlarm(calSet, false,notify1+1,SplitAlramVoice[1]);
                                         db.InsertNotifyId(notify1+1);
                                    }
                                    else if(splitrepeateddayname[i].equalsIgnoreCase("Thursday"))
                                    {
                                        dayofweek=5+(7-calSet.DAY_OF_WEEK);
                                        Cursor notifyvalue1=db.getnotifyid();
                                        if (notifyvalue1.moveToPosition(0))
                                        notify1=notifyvalue1.getInt(notifyvalue1.getColumnIndex("notifyvalue"));
                                        setAlarm(calSet, false,notify1+1,SplitAlramVoice[1]);
                                         db.InsertNotifyId(notify1+1);
                                    }   
                                    else if(splitrepeateddayname[i].equalsIgnoreCase("Friday"))
                                    {
                                        dayofweek=6+(7-calSet.DAY_OF_WEEK);
                                        Cursor notifyvalue1=db.getnotifyid();
                                        if (notifyvalue1.moveToPosition(0))
                                        notify1=notifyvalue1.getInt(notifyvalue1.getColumnIndex("notifyvalue"));
                                        setAlarm(calSet, false,notify1+1,SplitAlramVoice[1]);
                                         db.InsertNotifyId(notify1+1);
                                    }   
                                    else if(splitrepeateddayname[i].equalsIgnoreCase("Saturday"))
                                    {
                                        dayofweek=7+(7-calSet.DAY_OF_WEEK);
                                        Cursor notifyvalue1=db.getnotifyid();
                                        if (notifyvalue1.moveToPosition(0))
                                        notify1=notifyvalue1.getInt(notifyvalue1.getColumnIndex("notifyvalue"));
                                        setAlarm(calSet, false,notify1+1,SplitAlramVoice[1]);
                                         db.InsertNotifyId(notify1+1);
                                    }   
                                }
                            }

设置报警功能:

            Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
            intent.putExtra("NotifID", notifyid);
            intent.putExtra("Audio", AlarmAudio);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, PendingIntent.FLAG_CANCEL_CURRENT);
            AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
//          alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);

    if(repeat)
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
    else
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,targetCal.getTimeInMillis(),dayofweek*24*60*60*1000,pendingIntent);

报警接收器类:

int notifID = intent.getExtras().getInt("NotifID");
     String foraudio= intent.getExtras().getString("Audio");

      NotificationManager nm;
      nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);        
      CharSequence from = "My Dua";
      CharSequence message = "Cheking By Ismail";
      PendingIntent contentIntent = PendingIntent.getActivity(context, 0,new Intent(), 0);
      Notification notif = new Notification(R.drawable.ic_launcher,"Mohamed Ismail", System.currentTimeMillis());
      notif.setLatestEventInfo(context, from, message, contentIntent);

      Uri audio=Uri.parse("mnt/sdcard/sounds/"+foraudio+".mp3");
      notif.sound=audio;
      nm.notify(notifID++, notif);  

最佳答案

为通知创建一个唯一的 id 并像这样传递:

 Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
        intent.putExtra("NotifID", notifyid);
        intent.putExtra("Audio", AlarmAudio);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), notifyid, intent, PendingIntent.FLAG_one_shot);
        AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

关于java - 如何使用通知播放多个闹钟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13680784/

相关文章:

java - GCM CCS 服务器实现未接收上游消息

android - 如何在我的应用程序中实现 AccountManager

c - 调用函数 popen 中断进程

iphone - 使用 EKEventEditViewController 创建带有闹钟的日历事件

c - linux C语言。 setjmp longjmp 报警

java - Android、SQL、空指针异常

java - Python 解析 Java Double.toHexString(double) 的输出

java - 具有一对多关联的 Hibernate 条件

android - 如何始终显示滚动条

Android 4.0.3模拟器没有su