Please excuse me if this is a noob question. I have tried every possibility I could to set five alarms daily from the five edit texts. But nothing worked! I also have a button (not shown in this code) which updates these edit texts (therefore should update the alarm times as well). Here’s my code:
for (int i = 0; i < 5; i++) {
switch (i) {
case 0:
fajr.setText(result[i]);
tFajr = new GregorianCalendar();
tFajr.set(year, month, day,
Integer.parseInt(result[i].substring(0, 2)),
Integer.parseInt(result[i].substring(3, 5)));
break;
case 1:
zuhr.setText(result[i]);
tZuhr = new GregorianCalendar();
tZuhr.set(year, month, day,
Integer.parseInt(result[i].substring(0, 2)),
Integer.parseInt(result[i].substring(3, 5)));
break;
case 2:
asr.setText(result[i]);
tAsr = new GregorianCalendar();
tAsr.set(year, month, day,
Integer.parseInt(result[i].substring(0, 2)),
Integer.parseInt(result[i].substring(3, 5)));
break;
case 3:
maghrib.setText(result[i]);
tMaghrib = new GregorianCalendar();
tMaghrib.set(year, month, day,
Integer.parseInt(result[i].substring(0, 2)),
Integer.parseInt(result[i].substring(3, 5)));
break;
case 4:
isha.setText(result[i]);
tIsha = new GregorianCalendar();
tIsha.set(year, month, day,
Integer.parseInt(result[i].substring(0, 2)),
Integer.parseInt(result[i].substring(3, 5)));
break;
}
}
P.S: fajr,zuhr,asr,maghrib,isha are the five EditTexts. I tried to use a pending intent and an alarm manager to fire the alarms but it didnt work. Does any one have a good suggestion?
First of all you need to declare a pending intent for each of the alarms. So if you want 5 alarms you will need to run it 5 times
and the intent_code should change as well. Every time you register a new one you have to use a different code. In my application I have a random number generated every time that is executed. You can also pass data to your notification trough here using the Intent. Mind the difference between Intent and PendingIntent.
After that you need to register your alarm. Again 5 times, one for each alarm you want to trigger.
You need a BroadcastReceiver to receive your alarm and display the notification. I am pasting my whole class. This will be triggered after the time set in the Time_in_milis_from_now_till_your_alarm. And you can run pretty much whatever you like in here. I don’t know what kind of alarm you want, in my case I’m using a notification. You can find the details about the notification here and here.
Last, don’t forget to declare your broadcast in the manifest or it wont work. This goes inside application tag.
If you have any problems with the alarm manager methods you can find the documentation here.
All the best!
Edit
To play a sound use the MediaPlayer as recomended by the developer’s guide. Keep the file mysound.mp3 in your folder /res/raw. And you just call the following method in your BroadcastReceiver!