I am developing an app that plays a song on a certain date and time, it can be a day at different times so it does not always run on the same date, I created a method to load_date_date (); that I create an alarm with AlarmManager and then it is received with the AlarmReceiver class that extends BroadcastReceiver, and that's where it calls another class to use the mediaplayer. This app is going to be used indefinitely and can be set to sound audios on a specific date for a few months. My question is .. Is the approach appropriate, or is it better to use a Service for this case? Would it be more correct to use a Timer to create the alarms?.
Thanks in advance.
private void cargar_fechas_temporizador() {
Date fecha = new Date();
for (int e=0; e<list_audios.size();e++){
if (fecha.compareTo(list_audios.get(e).getFecha_reproduccion()) < 0) {
crear_alarma_audio(list_audios.get(e).getFecha_reproduccion(),
list_audios.get(e).getHora_fin(),list_audios.get(e).getPath_audio(),
list_audios.get(e).getNum_veces(), list_audios.get(e).getAtenuacion(), e);
}
}
}
The create alarm method ..
private void crear_alarma_audio(Date fecha_reproduccion, String hora_fin, String audio, String repeticion, String atenuacion, int num_intent ) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(fecha_reproduccion);
myIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
myIntent.putExtra("audio", audio);
myIntent.putExtra("repeticion", repeticion);
myIntent.putExtra("atenuacion", atenuacion);
pending_intent = PendingIntent.getBroadcast(MainActivity.this, num_intent, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pending_intent);
}
and the AlarmReceiver class ...
public class AlarmReceiver extends BroadcastReceiver {
private Context context;
@Override
public void onReceive(Context context, Intent intent) {
String audio = intent.getExtras().getString("audio");
String num_veces = intent.getExtras().getString("repeticion");
String atenuacion = intent.getExtras().getString("atenuacion");
Integer num_intent = intent.getExtras().getInt("num_intent");
MediaPlayerHolder mp = new MediaPlayerHolder(context);
mp.initializeMediaPlayer();
mp.loadMedia(Uri.parse(audio));
mp.isPlaying();
mp.play();
}
}