I have an app with two activities: The first activity has a 'Send' button. The second activity appears when Send is pressed.
As an exercise I want to have Send also set an alarm on the phone (say, at 5:30am) before starting the second activity. Here's the method that runs when the button is pressed:
I got the alarm-setting code from developer.android.com/guide/components/intents-common.html#Clock
When I run the above method, it's as if the 'Make alarm' code isn't there. Pressing Send simply starts the second activity, and no alarm is set in the clock app
When I comment out the 'Start second activity' code, the button sets an alarm for 5:30am (
) and shows the alarms list in the clock app, with the new alarm listed. But of course my app's second activity never starts and the clock app remains in the foreground
(I want the alarm to be set in the background).
When I comment in the Thread.sleep(1000) call (and comment back in the 'Start second activity' code), the method runs as intended. Pressing Send causes the clock app to quickly flash on screen before the second activity of my app comes into the foreground. A system message appears saying "An alarm has been set for 5:30 am", and I can verify this by going into the clocks app.
I'm confused. What difference could the Thread.sleep(1000) call be making? When I change it to sleep for only 10 milliseconds, I get the same results as when the Thread.sleep call wasn't there at all. I haven't read any Android documentation recommending putting a delay after starting an activity.
As an exercise I want to have Send also set an alarm on the phone (say, at 5:30am) before starting the second activity. Here's the method that runs when the button is pressed:
Code:
public void sendMessage(View view) throws InterruptedException {
// Make alarm
Intent i = new Intent(AlarmClock.ACTION_SET_ALARM)
.putExtra(AlarmClock.EXTRA_HOUR, 5)
.putExtra(AlarmClock.EXTRA_MINUTES, 30);
startActivity(i);
//Thread.sleep(1000);
// Start second activity
Intent intent = new Intent(this, DisplayMessageActivity.class);
startActivity(intent);
}
I got the alarm-setting code from developer.android.com/guide/components/intents-common.html#Clock
When I run the above method, it's as if the 'Make alarm' code isn't there. Pressing Send simply starts the second activity, and no alarm is set in the clock app

When I comment out the 'Start second activity' code, the button sets an alarm for 5:30am (
) and shows the alarms list in the clock app, with the new alarm listed. But of course my app's second activity never starts and the clock app remains in the foreground
(I want the alarm to be set in the background).When I comment in the Thread.sleep(1000) call (and comment back in the 'Start second activity' code), the method runs as intended. Pressing Send causes the clock app to quickly flash on screen before the second activity of my app comes into the foreground. A system message appears saying "An alarm has been set for 5:30 am", and I can verify this by going into the clocks app.
I'm confused. What difference could the Thread.sleep(1000) call be making? When I change it to sleep for only 10 milliseconds, I get the same results as when the Thread.sleep call wasn't there at all. I haven't read any Android documentation recommending putting a delay after starting an activity.