Programar una matriz de alarmas en Android

I am making a personal organizer. From the calendar the user can choose the day and then can make a date with a specific hour. At this date and hour the application should show an other activity, which is the notification. User can make just one note for day. So the pk of each note is YYYYMMDD.

So, I wants to know that what I've done is right or not. I've a service which is started with application. The onStartCommand of the service checks if in the current date there is a note in the db and, if there is, he calls the Activity Notification. I've a runnable thread which is looping each minute and it update the date if the service, because the date of the onStartCommand is static. If the date getted by the runnable is equal to onStartCommand's date then keep cycling, else I start the service again with:

Intent myIntent = new Intent(Receiver.this, NotificaSuoneria.class);
startActivity(myIntent);

In this way I haven't any troubles, but I want to know if the runnable thread could be killed by android, because if killed I can't check the alarms of the others days.

And, when I restart the service each time update the date, the old runnable thread will be killed or will were a lot of threads?

P.S. I don't use the AlarmManager for schedule the alarm. If the datenow have some engagement with alarms I calculate the time left and sleep for this time, then I'll start the notification activity. Sorry for my poor english.

preguntado el 29 de julio de 12 a las 11:07

1 Respuestas

You should use AlarmManager for this with a loop like following edited

try {
            cntxt = createPackageContext("your.app.package",CONTEXT_IGNORE_SECURITY);
           } catch (NameNotFoundException e) {
              // handle exception here
              e.printStackTrace();
           }

Intent myIntent = new Intent(cntxt , yourreciever.class);
    PendingIntent pi;
    AlarmManager alarmManager;
        for(int i=0;i<=timesinday;i++)
        {
        pi=PendingIntent.getBroadcast(cntxt, i,myIntent, 0);

        alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                                        alarmManager.set(AlarmManager.RTC_WAKEUP, currentcal.getTimeInMillis(),pi);

        }

Do not forget to add NotificaSuoneria in Android Menifest as a Reciever. If you you want alarm to goes off every day you should use

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,todaycal.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);

@Meesta I am editing the answer you do not have to mention activity here Intent myIntent = new Intent(Receiver.this, here.class); here will be the reciever class from wher you can start your activity also see http://androidword.blogspot.in/2010/10/how-to-use-broadcast-receiver.html and try to add flag_new_activitywhen starting the activity from broadcast reciever

Respondido 04 ago 12, 14:08

In the above code if you put the array of timesinmillis where there is currentcal.getTimeInMillis().Like times[i] you can get what you required - Nitin

Thanks for the answer.. I done what u told me, but if the PendingIntent is getBroadcast, I get nothing. and if I change to getActivity (notificaSuoneria is an activity) the alarm will instant start - Meesta

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.