0

I'm setting multiple alarms on repeat (everyday) on a button click. they are working fine if all of the time is greater then the time of button press.But if any time in the array has passed, it is generating the notification right after the button click and as many as passed. I have tried to put a condition to check if time has passed so set the alarm on the next day but it is not working I keep getting notification on the button click.

here is my mainActivity code for button click

 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

and here Is my alertReciver code

 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
           }
shahtaj khalid
  • 476
  • 7
  • 24
  • Your comparison is a little off. You're comparing an hour to a time in milliseconds. Check how it's done on the linked duplicate. And you'll want to do that comparison before you assign the value to `AllTime[i]`. – Mike M. Jan 28 '17 at 10:00
  • @MikeM. It's not working for me I tried !! – shahtaj khalid Jan 28 '17 at 10:23
  • Update your post with your current code. – Mike M. Jan 28 '17 at 10:28
  • @MikeM. just updated, check. and I'm using both comparisons now editing my first one, still not working. – shahtaj khalid Jan 28 '17 at 10:38
  • 1
    Remove the `if ( curHr > h[i]) {...}` block, and move `AllTime[i] = calendar.getTimeInMillis();` to after `if(calendar.before(Calendar.getInstance())) {...}`. – Mike M. Jan 28 '17 at 10:38
  • 1
    @MikeM. Stupid me !!! thanks a lot , it worked :D – shahtaj khalid Jan 28 '17 at 10:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/134239/discussion-between-shahtaj-khalid-and-mike-m). – shahtaj khalid Jan 28 '17 at 11:18
  • @MikeM. hey I got a little problem here wondering if you can help with that too, now according my code these notifications should be repeated on the next day but none of it are repeating. – shahtaj khalid Jan 30 '17 at 07:46
  • Is your device rebooting? Alarms don't persist through a reboot. You have to set them again at boot. Also, since API 19, if your `targetSdkVersion` is 19+, `setRepeating()` is not exact, so the alarm might not fire until a while after the set time. – Mike M. Jan 30 '17 at 07:51
  • @MikeM. No I didn't rebooted the device. and yes my target is 23 but as setRepeating() is not exact, still it should repeat after some time? but it is not repeating any of them at all. – shahtaj khalid Jan 30 '17 at 08:04
  • Are you forcibly stopping your app? If an app is force closed, it's put back into the _stopped_ state, and the manifest Receivers won't work again until the app is run next. Or maybe your device has some sort of aggressive power management process that might be interfering. You might check through the device Settings, or any management or administrative-type app. Beyond that, all I could suggest is to test with shorter intervals; make sure that works, at least. Also, you can use adb to check the status of your alarms after a while; e.g., the next day: http://stackoverflow.com/q/28742884. – Mike M. Jan 30 '17 at 08:16
  • @MikeM. thanks , I will try using adb shell to check if it even sets the alarm or not. and no i'm not forcibly stopping the app. one more thing can you mention how and where to actually use this command as in the link it is asking about the output of the command, and so sorry if i'm annoying you :p – shahtaj khalid Jan 30 '17 at 08:51
  • It's a command line tool. There's a pretty extensive write-up about it here: https://developer.android.com/studio/command-line/adb.html. – Mike M. Jan 30 '17 at 08:57
  • @MikeM. I tried, and hey I have received a notification today out of nowehere, I think it is one of the repeating ones as I keep changing the time to test.. have a look http://stackoverflow.com/questions/41942221/how-to-read-adb-dumpsys-alarm-output – shahtaj khalid Jan 30 '17 at 17:49

0 Answers0