Android: Alarm to be play every 30 minutes and it start from 12:30
Asked Answered
E

4

1

Here i am going to use the alarm service to play the alarm at every 30 minutes. Right now i have set it to play it at every 10 second from the Every start.

Here is the Code:

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings_layout);


    Intent myIntent = new Intent(SettingsActivity.this, MyAlarmService.class);
    pendingIntent = PendingIntent.getService(SettingsActivity.this, 0, myIntent, 0);

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10*1000, pendingIntent);

}

Now the Problem is, I want to start the alarm from the 12:30 not from the time application start and it should repeatedly play at evert 30 minutes. like 1:00, 1:30, 2:00 . . . etc

So what changes i have to do in my code ?

Esbjerg answered 6/4, 2012 at 5:38 Comment(8)
You have to create a service that will run in your application background. So, if you exit exit your app, the service will stay running and execute your code stuff according your logic.Platelet
@Suvam Roy i kknow that all. But want to know about how to set that the alarm should be start from the 12:30 and play at every 30 minutes.Esbjerg
@iDroidExplorer : see this post maybe helpfulUsing Alarmmanager to start a service at specific timeAccede
@imrankhan: hey, thanks. I think it will be works. But just ask you question . if i have set the hour as 12 and minute as 30 and if i install the app in device at 1:29 then will it be play the alarm at 1:30 ???Esbjerg
@imrankhan: Please put your answer hear so i can accept it.Esbjerg
@iDroidExplorer :Thanks, what happening in this case : if i have set the hour as 12 and minute as 30 and if i install the app in device at 1:29 then will it be play the alarm at 1:30 ?Accede
@I have update the code little and now it run as i want. I have also set the condition like while the service will call, there i have put the condition of the time like if the time is 12:30 then it will play the Alarm otherwise not. So i have solve the Issue like that.Esbjerg
@iDroidExplorer : that's great.sorry for late response ,i have not receive your text bez u miss my name after @Accede
P
1

Set your initial alarm time for 12:30 using the Set method.

When the alarm fires, then set up your next alarm time and keep doing that until you don't want the alarm any more.

You don't need a service to do such a simple task. AlarmManager is more than capable of handling this.

Propene answered 6/4, 2012 at 5:55 Comment(0)
P
1

Try it

And Use first time -

Try another

Platelet answered 6/4, 2012 at 6:28 Comment(0)
T
1

A naive approach would be:

  1. Start a context that will always run, such as a Thread in a Service.
  2. Calculate the timestamp of the time you want the next alert to ring using Date and put it in "long alertTimestamp".
  3. In a loop, calculate the timestamp right now using Date and put it in "long nowTimestamp" .
  4. If nowTimestamp < alertTimestamp, put the thread to sleep for (alertTimestamp - nowTimestamp).
  5. Else sound the alert and recalculate alertTimestamp, repeat.

Make sure you catch notify interruptions to the tread gracefully, that is the key to aborting the timer.

Hope this was helpful.

Trenna answered 6/4, 2012 at 6:50 Comment(0)
B
1

For repeating every 30 minutes you will need to add this to your code:

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*60*30, pendingIntent); //millisecs*seconds*minutes

But I haven't figure out how to start at specific time yet.

Battista answered 8/12, 2013 at 13:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.