如果时间已过,请在第二天设置通知

时间:2017-01-28 09:56:16

标签: android notifications calendar alarmmanager repeatingalarm

我在点击按钮时重复(每天)设置多个闹钟。如果所有时间都大于按下按钮的时间,它们工作正常。但是如果数组中的任何时间已经过去,它会在按钮单击后立即生成通知,并且通过多次。 我试图设置一个条件来检查时间是否已经过去,所以在第二天设置闹钟但是它无法正常工作我不断收到按钮点击的通知。

这是按钮点击的主要活动代码

 int[] h = new int[C.getCount()];
    int[] m = new int[C.getCount()];
    long[] AllTime = new long[C.getCount()];


    Intent alertIntent = new Intent(this, AlertReceiver.class);
    alertIntent.putExtra("strings", DealTimes);  // sending deal times thoruh string to AlertReceverrr !!!

    for (int i = 0; i < C.getCount(); i++) {


        h[i] = Integer.valueOf(DealTimes[i].split(":")[0]);
        m[i] = Integer.valueOf(DealTimes[i].split(":")[1]);
        //   Toast.makeText(getBaseContext(), "Time at 16th index " + h + "min " + m, Toast.LENGTH_SHORT).show();

        Calendar calendar = Calendar.getInstance();

        int curHr = calendar.get(Calendar.HOUR_OF_DAY);

        calendar.set(Calendar.HOUR_OF_DAY, h[i]);
        calendar.set(Calendar.MINUTE, m[i]);
        calendar.set(calendar.SECOND, 0);
        calendar.set(calendar.MILLISECOND, 0);
        AllTime[i] = calendar.getTimeInMillis();

       // checking if current time is greater the any of the time in array
         if(calendar.before(Calendar.getInstance())) {
            calendar.add(Calendar.DATE, 1);
        }

        if ( curHr > h[i])
        {
            calendar.add(Calendar.DATE, 1);
        }    

        Toast.makeText(getBaseContext(), "Setting Alarm" + i, Toast.LENGTH_SHORT).show();
        final int _id = (int) System.currentTimeMillis();

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    //    alarmManager.set(AlarmManager.RTC_WAKEUP, AllTime[i],
      //          PendingIntent.getBroadcast(this, _id, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,AllTime[i],AlarmManager.INTERVAL_DAY,
                PendingIntent.getBroadcast(this, _id, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
        // recinving noti within 30 seconds of button press, and generting as much noti as are passed. DUH. was reciving right on time witout delay before setting on repeat

这里是我的alertReciver代码

 public void onReceive(Context context, Intent intent) {



    String[] myStrings = intent.getStringArrayExtra("strings");
    Log.i("okk", "cathing intent values through break" + Arrays.toString(myStrings));


    createNotification(context,"Time is here baby","5 sec hogae hen beta","Alert");
    Log.i("okk", "cathing intent values through break" + Arrays.toString(myStrings));


}

public void createNotification(Context context,String msg,String msgText, String msgAlert)
{
    final int _id = (int) System.currentTimeMillis();  // unique request code

                                                                          PendingIntent notificationIntent = PendingIntent.getActivity(context,_id, new Intent(context,MainActivity.class),0);  // changed from 0 to _id

    NotificationCompat.Builder mbuilder= new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.cast_ic_notification_play)
            .setContentTitle(msg)
            .setTicker(msgAlert)
            .setContentText(msgText);

    // now intent we want to fire when noti is clicked

    mbuilder.setContentIntent(notificationIntent);

    // how person is notified

    mbuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);

    mbuilder.setAutoCancel(true); 
    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService((Context.NOTIFICATION_SERVICE));

//    Log.i("okk", "NOTIFIED " + intent.getExtras());

    notificationManager.notify(1,mbuilder.build());  // changes from 1 to _id
           }

0 个答案:

没有答案