1

Building my own alarm clock app for android and have some trouble with notification. I want to set nap time from notification, so I build notification with action

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    addNotification();
}

    private void addNotification() {
        SimpleDateFormat sdfHour = new SimpleDateFormat("HH");
        SimpleDateFormat sdfMinute = new SimpleDateFormat("mm");
        int timeHour = Integer.parseInt(sdfHour.format(getInstance().getTime()));
        int timeMinute = Integer.parseInt(sdfMinute.format(getInstance().getTime()));

        Intent a15 = new Intent(AlarmClock.ACTION_SET_ALARM);
        a15.putExtra(AlarmClock.EXTRA_MESSAGE, "Nap");
        a15.putExtra(AlarmClock.EXTRA_HOUR, timeHour);
        a15.putExtra(AlarmClock.EXTRA_MINUTES, timeMinute+15);
        a15.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
        PendingIntent nap15 = PendingIntent.getActivity(this,1,a15,PendingIntent.FLAG_UPDATE_CURRENT);

        Intent a30 = new Intent(AlarmClock.ACTION_SET_ALARM);
        a30.putExtra(AlarmClock.EXTRA_MESSAGE, "Nap");
        a30.putExtra(AlarmClock.EXTRA_HOUR, timeHour);
        a30.putExtra(AlarmClock.EXTRA_MINUTES, timeMinute+30);
        a30.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
        PendingIntent nap30 = PendingIntent.getActivity(this,2,a30,PendingIntent.FLAG_UPDATE_CURRENT);

        Intent a45 = new Intent(AlarmClock.ACTION_SET_ALARM);
        a45.putExtra(AlarmClock.EXTRA_MESSAGE, "Nap");
        a45.putExtra(AlarmClock.EXTRA_HOUR, timeHour);
        a45.putExtra(AlarmClock.EXTRA_MINUTES, timeMinute + 3);
        a45.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
        PendingIntent nap45 = PendingIntent.getActivity(this,3,a45,PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_alarm_white_24dp)
                .setContentTitle("Take a nap.")
                .setContentText("Select nap time.")
                .addAction(R.drawable.ic_alarm_add_white_24dp, "15min",nap15)
                .addAction(R.drawable.ic_alarm_add_white_24dp, "30min",nap30)
                .addAction(R.drawable.ic_alarm_add_white_24dp, "45min",nap45);


        // Add as notification
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(0, mBuilder.build());

    }

but when I select nap time for 15min alarm clock set up for 15 min from time when i created notification instead of current system time. For example if i run app at 5 pm, and after 10min (5:10pm) I choose action from notification it's set alarm for 5 pm + 15 not for 5:10pm + 15. So it is possible to get current time when i clicked action in notification ? what i need to use ?

Mich
  • 71
  • 13

1 Answers1

0

You could get the current time in hours and minutes from SimpleDateFormat:

SimpleDateFormat sdfHour = new SimpleDateFormat("HH");
SimpleDateFormat sdfMinute = new SimpleDateFormat("mm");
int timeHour = Integer.parseInt(sdfHour.format(getInstance().getTime()));
int timeMinute = Integer.parseInt(sdfMinute.format(getInstance().getTime()));

so it would be:

Intent a15 = new Intent(AlarmClock.ACTION_SET_ALARM);
        a15.putExtra(AlarmClock.EXTRA_MESSAGE, "Nap");
        a15.putExtra(AlarmClock.EXTRA_HOUR, timeHour);
        a15.putExtra(AlarmClock.EXTRA_MINUTES, timeMinute + 15);
        a15.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
        PendingIntent nap15 = PendingIntent.getActivity(this,1,a15,PendingIntent.FLAG_UPDATE_CURRENT);
buradd
  • 1,271
  • 1
  • 13
  • 19
  • doesn't work for me. Still get current time from first app run, and when i clicked action on notification time doesnt update to current value – Mich Mar 27 '17 at 20:21
  • make sure you are instantiating the int timeHour and int timeMinute at the time the action button is clicked, rather than when the activity is started – buradd Mar 27 '17 at 20:35
  • Now I instantiating both valur when I create notification, how I can instatiating both value at addAction(int, CharSequence, PendingIntent) in notification. Where I need to instatiatig values to do it right ? – Mich Mar 27 '17 at 20:48
  • can you post more code from your activity/fragment? – buradd Mar 27 '17 at 21:19
  • if the answer was helpful at all, please upvote or accept answer thank you! – buradd Mar 28 '17 at 15:30