0

I want to set two alarms in my app through intent.This is how the app should work, it should get time form the user and set alarm for that time +3 minutes.For instance, if the user wants to set an alarm for 8:30 it should set alarm for 8:30, and then for 8:33 immediately.I've used 3 as an example, but in the actual functioning, I will include a variable there.This app is to set multiple alarms with a gap that user enters. It will prompt the user about the number of such alarms to set.So I wrote two intents for it but only first one is working.This is my code:

Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM);

    intent.putExtra(AlarmClock.EXTRA_MESSAGE, "Your alarm");
    intent.putExtra(AlarmClock.EXTRA_HOUR, result);
    intent.putExtra(AlarmClock.EXTRA_MINUTES, i);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
        } else {
          Toast.makeText(getApplicationContext(), "There is alarm option for your device", Toast.LENGTH_SHORT).show();
       }

    Intent intent1 = new Intent(AlarmClock.ACTION_SET_ALARM);

     intent1.putExtra(AlarmClock.EXTRA_MESSAGE, "Your alarm");
     intent1.putExtra(AlarmClock.EXTRA_HOUR, result);
     intent1.putExtra(AlarmClock.EXTRA_MINUTES, i+3);
     if (intent1.resolveActivity(getPackageManager()) != null) {
         startActivity(intent1);
        } else {
           Toast.makeText(getApplicationContext(), "There is alarm option for your device", Toast.LENGTH_SHORT).show();
        }

Can we make intent object as an array?

Thanks You for the help sir. Here is what I did.

 @Override
  public void onResume()
 { super.onResume();
   secondintent();
 }


public void secondintent() {
        if (test == 1)
        {
            Intent intent1 = new Intent(AlarmClock.ACTION_SET_ALARM);

        intent1.putExtra(AlarmClock.EXTRA_MESSAGE, mess);
        intent1.putExtra(AlarmClock.EXTRA_HOUR, result);
        intent1.putExtra(AlarmClock.EXTRA_MINUTES, i + 5);
        startActivity(intent1);
        i=i+5;
        }
anjani
  • 45
  • 9
  • You are hard coding the 3 minutes that's not a good approach..what exactly you want to achieve??Every 3 minutes it should set the alarm..as in 8:33 8:36 ...so on?? – Akshay Jun 20 '17 at 09:37
  • I gave it just as an example it will take that value from the user. Do you remember thinking of waking up early and didn't with this we can put multiple alarms with a gap given by the user and the user can select no of alarms to set. – anjani Jun 20 '17 at 09:52
  • Yeah..but say user enters 5 min so it will trigger every 5 min??8:80 8;40 8:50...?? – Akshay Jun 20 '17 at 09:53
  • The user also enters the If the user enters 5 mins and 3 in frequency(There is a frequency text box in app's UI) it will trigger for 8:30 8:35 8:40 – anjani Jun 20 '17 at 09:55

2 Answers2

1

You can not go back to your main or previous activity once you've performed startActivity(firstIntent) unless you'll finish() the next activity or you hit the back button. Every time you go back to your previous activity, it's onResume() will be called and in order to continue setting up your next alarm, you have to invoke startActivity(nextIntent) again. Think of that startActivity(firstIntent) as a break statement for your method.

So what you can do is, try to set the next alarm from your onResume().

Read about Activity-lifecycle concepts here

Daksh Gargas
  • 2,567
  • 1
  • 15
  • 28
  • [link](http://example.com)_italic_**bold** `@Override public void onResume() { super.onResume(); void secondintent(); }` Is this how I should do it? – anjani Jun 21 '17 at 05:41
  • 1
    annotations are not allowed here. – anjani Jun 21 '17 at 05:46
  • what's with that **link_italic_bold** in your comment? Is that in your code as well? – Daksh Gargas Jun 21 '17 at 05:48
  • Hmm, it's pretty weird that you're getting this error. It means that `@Override` annotation in not allowed in your class, but your compiler should coz your class `extends AppCompatActivity`/`Activity`. Can I have a screenshot of your code and log as well? I guess it might be some sort of syntax error or problem with the scope(of the method). – Daksh Gargas Jun 21 '17 at 05:53
  • Thank you sir it is working now. I called the wrongly now it is working fine. – anjani Jun 21 '17 at 05:59
0

I think you should change your code use alarm manager for multiple alarm if i'm right than use
This link

Dipesh
  • 36
  • 3