I’m trying to trigger 3 alarms at different times using alarm manager. Here’s my code (Note that alarm1, alarm2, alarm3 are three calender objects set earlier in my code):
AlarmNum=1;
new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
Intent myIntent = new Intent(MainActivity.this,
MyAlarmService.class);
pendingIntent = PendingIntent.getService(MainActivity.this, 0,
myIntent, 0);
if (AlarmNum == 1)
alarmManager.set(AlarmManager.RTC_WAKEUP,
alarm1.getTimeInMillis(), pendingIntent);
else if (AlarmNum == 2)
alarmManager.set(AlarmManager.RTC_WAKEUP,
alarm2.getTimeInMillis(), pendingIntent);
else
alarmManager.set(AlarmManager.RTC_WAKEUP,
alarm3.getTimeInMillis(), pendingIntent);
Toast.makeText(MainActivity.this, "Start Alarm",
Toast.LENGTH_LONG).show();
}
};
In the above code I start an intent which provokes MyAlarmService class given below:
public class MyAlarmService extends Service {
MainActivity instance;
MediaPlayer mp;
@Override
public void onCreate() {
// TODO Auto-generated method stub
Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG)
.show();
mp = MediaPlayer.create(this, R.raw.alarmtone);
instance = new MainActivity();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG)
.show();
return null;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG)
.show();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG)
.show();
mp.start();
instance.setAlarmNum(instance.getAlarmNum() + 1);
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
mp.release();
mp.reset();
Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG)
.show();
return super.onUnbind(intent);
}
}
I assume there is a problem here because the toasts never show up and neither does the alarm.
What I understood from your code and comments is that you want first alarm to trigger the second and second alarm trigger the third, etc.
Several problems I notice with your code.
1) This is a wrong way to start an activity, it won’t work:
Instead, you should do this:
Regardless of above issue, you do not have to start your MainActivity everytime your alarm is triggered. You can just trigger the new alarm inside your service. Here, I am assuming that 2nd alarm trigger time is later than the first, and third later than the second, etc. Otherwise, your algorithm won’t work.
First alarm will be set in your activity:
Note that I added an extra integer to your intent so that your service will be able to understand which alarm was set previously.
Back in your service onBind function, you should read this extra from your Intent.
Then, check the value of AlarmNum and set the next alarm the same way we did, depending on the value (if AlarmNum == 1, set the second alarm, incerementing your intent’s extra by 1, etc.). Since you have 3 alarms to set, if it reads 3, then you will do nothing but complete your service.
As a side notice, it is a better practice to show toast messages in your Service using a Handler.