2014-11-11 12 views
6

Mam scenariuszJak ustawić alarm dla dni roboczych w android

ustawienie alarmu (od poniedziałku do piątku)

powiedzmy wybrać czas, który jest: hour = 9, minutes = 15, am_pm = "AM".

Teraz chcę, aby ustawić alarm dla każdego Monday to Friday at 9:15 AM

kodeksu poniżej Próbowałem, ale nie otrzymania pożądanego rezultatu.

if(choice.equals("Week Days (Mon-Fri)")) 
{ 
    for(int a = 2; a <= 5; a++) //here I am assuming a is from 2 to 5 (calendar DAY_OF_WEEK from Monday to Friday) 
    { 
     Calendar alarmCalendar = Calendar.getInstance(); 

     alarmCalendar.set(Calendar.HOUR_OF_DAY, _hourOfDay); 

     alarmCalendar.set(Calendar.MINUTE, _minute); 

     alarmCalendar.set(Calendar.SECOND, 0); 

     alarmCalendar.set(Calendar.MILLISECOND, 0); 

     if(am_pm.equals("AM")) 
     { 
      alarmCalendar.set(Calendar.AM_PM, 0); 
     } 
     else 
     { 
      alarmCalendar.set(Calendar.AM_PM, 1); 
     } 


     alarmCalendar.set(Calendar.DAY_OF_WEEK, a); 

     Long alarmTime = alarmCalendar.getTimeInMillis(); 

     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
       alarmTime, 24 * 60 * 60 * 1000 , pendingIntent); 
    } 

    Toast.makeText(ActivityReminder.this, "Meeting Reminder Set on Week Days (Mon-Fri)", 
         Toast.LENGTH_LONG).show(); 
} 

Użyłem BroadcastReceiver jak:

public class AlarmReceiver extends BroadcastReceiver 
{ 
    NotificationManager mNotificationManager; 

    Context context; 

    public static final String TAG = "Reminder..."; 

    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     this.context = context; 

     String subject = "<h3>Meeting Reminder: </h3>" + intent.getStringExtra("subject"); 

     Toast.makeText(context, Html.fromHtml(subject), Toast.LENGTH_LONG).show(); 

     showNotification(subject); 
    } 

    @SuppressWarnings("deprecation") 
    private void showNotification(String msg) 
    { 
     Intent notificationIntent = new Intent(context.getApplicationContext(), 
       ActivityMainScreen.class); 

     TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 

     stackBuilder.addParentStack(ActivityMainGeofence.class); 

     stackBuilder.addNextIntent(notificationIntent); 

     String arrivalTime = TimeUtil.toString(Calendar.getInstance().getTime(), 
       "dd-MM-yyyy hh:mm:ss a"); 

     notificationIntent.putExtra("subject", msg) 
     .putExtra("time", arrivalTime).putExtra("type", "Meetings/Reminder"); 

     PendingIntent notificationPendingIntent = stackBuilder 
       .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

     Notification notification = new Notification(); 
     notification.icon = R.drawable.app_icon; 

     notification.setLatestEventInfo(context.getApplicationContext(), 
       "WFM Meeting Reminder", Html.fromHtml(msg), notificationPendingIntent); 

     notification.flags |= Notification.FLAG_AUTO_CANCEL; 

     notification.defaults |= Notification.DEFAULT_SOUND; 

     notification.defaults |= Notification.DEFAULT_VIBRATE; 

     NotificationManager mNotificationManager = (NotificationManager) 
       context.getSystemService(Context.NOTIFICATION_SERVICE); 

     mNotificationManager.notify(0, notification); 

    } 
} 

Ostatecznie w manifeście:

<receiver android:name="com.my_package_name.AlarmReceiver" > 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      </intent-filter> 
</receiver> 

edycja: Tutaj chcę powiedzieć, że One Time Alarm pracował dla mnie, ale dla powyższy wybór Week Days (Mon-Fri) nie działa poprawnie

Kodeks pracy jest:

if(choice.equals("One Time")) 
{ 
    alarmManager.set(AlarmManager.RTC_WAKEUP, PERIOD, pendingIntent); // here PERIOD is the time selected by me in milliseconds... 

    Toast.makeText(ActivityReminder.this, "Meeting Reminder Set One Time", 
      Toast.LENGTH_LONG).show(); 
} 

Gdzie robię źle. Każda pomoc będzie doceniona.

Odpowiedz

5

Przypuszczam, że alarm zostanie ustawiony tylko na piątek o 9:15? Powinno to być ze względu na następującą linię w docs AlarmManager:

If there is already an alarm scheduled for the same IntentSender, it will first be canceled. http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating(int, dawno, dawno, android.app.PendingIntent)

Aby to zrobić, co chcesz, to trzeba albo chcesz 5 PendingIntents lub po prostu ustaw alarm dla pierwszego zdarzenia, a kiedy ten alarm zostanie odebrany, ustawisz alarm na następny dzień i tak dalej.

Prawdopodobnie skorzystam z drugiej opcji, ponieważ w pierwszej metodzie potrzebne są 5 różnych PendingIntents, co oznacza, że ​​ich requestCode lub zamiar tworzenia kopii musi być inny (z innym rodzajem, działaniem lub kategorią).

2

Sprawdź stronę dla AlarmManager i spójrz na notatkę w setRepeating(). Od wersji API 19 funkcje powtarzające są niedokładne. Aby uzyskać poprawne powtórzenie, musisz za każdym razem obsługiwać alarm i przestawiać go ponownie.

3

Spróbuj jej kod pracował dla mnie ...

for (int k = 2; k < 5; k++) { 
        int sday = k; 
        Calendar today = Calendar.getInstance(); 
        Calendar AlarmDate = Calendar.getInstance(); 
        AlarmDate.set(Calendar.HOUR_OF_DAY, inputH); 
        AlarmDate.set(Calendar.MINUTE, inputM); 
        AlarmDate.set(Calendar.SECOND, 0); 
        while (today.after(AlarmDate)) { 
         AlarmDate.add(Calendar.DAY_OF_MONTH, 1); 
        } 
        while (AlarmDate.get(Calendar.DAY_OF_WEEK) != sday) { 
         AlarmDate.add(Calendar.DAY_OF_MONTH, 1); 
        } 

        Intent i = new Intent(this, AlertPopUpActivity.class); 
        i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
        i.putExtra("uid", k + (alert.getId() * 1000)); //a unique id for alram 
        i.putExtra("id", alert.getId()); 
        i.putExtra("msg", alert.getEventName()); 

        if (minute < 10) 
         i.putExtra("time", hour + ":0" + minute); 
        else 
         i.putExtra("time", hour + ":" + minute); 
        i.putExtra("ampm", inputAMPM); 
        PendingIntent pi = PendingIntent.getActivity(this, 
          k + (alert.getId() * 1000), i,   //same unique id (used to update alram if canceled) 
          PendingIntent.FLAG_UPDATE_CURRENT); 

        am.set(AlarmManager.RTC_WAKEUP, AlarmDate.getTimeInMillis(), pi); 
        System.out.println("Set Time :" + AlarmDate.getTime()); 
       } 
2

) Przede wszystkim nie można mieć do pętli na samym oczekującego intencyjny, będzie anulować wcześniejsze przypadki.

) Rozwiązanie polega na ponownym ustawieniu alarmu za pomocą odbiornika wewnątrz metody onReceive, po zakończeniu alarmu.

) Oto pomysł, jak to osiągnąć, dołącz resztę kodu zgodnie z wymaganiami. Użyłem klasy kalendarza do ustawienia alarmu, aw piątek pomiń 2 dni pomiędzy.

void setAlarm() { 
    AlarmManager alarmManager = (AlarmManager) context 
      .getSystemService(Context.ALARM_SERVICE); 
    if (choice.equals("One Time")) { 
     alarmManager.set(AlarmManager.RTC_WAKEUP, PERIOD, pendingIntent); 

     Toast.makeText(context, "Meeting Reminder Set One Time", 
       Toast.LENGTH_LONG).show(); 
    } else if (choice.equals("Week Days (Mon-Fri)")) { 
     Calendar calendar = Calendar.getInstance(); 
     int day = calendar.get(Calendar.DAY_OF_WEEK); 

     switch (day) { 
     case Calendar.MONDAY: 
      alarmManager 
        .set(AlarmManager.RTC_WAKEUP, PERIOD, pendingIntent); 
     case Calendar.TUESDAY: 
      alarmManager 
        .set(AlarmManager.RTC_WAKEUP, PERIOD, pendingIntent); 
     case Calendar.WEDNESDAY: 
      alarmManager 
        .set(AlarmManager.RTC_WAKEUP, PERIOD, pendingIntent); 
     case Calendar.THURSDAY: 
      alarmManager 
        .set(AlarmManager.RTC_WAKEUP, PERIOD, pendingIntent); 
     case Calendar.FRIDAY: 
      alarmManager.set(AlarmManager.RTC_WAKEUP, PERIOD * 3, 
        pendingIntent); 
     } 
    } 
} 

) Dla "wyboru" warunku if-else może być konieczne przechowywanie tych wartości w innych miejscach, takich jak SharedPreferences.

+0

Załóżmy, że dziś jest poniedziałek. Jeśli podążę za twoim kodem, to w Kalendarzu Przełączników. MONDAY będzie prawdziwy, a alarmManager ustawi go na poniedziałek ... Dobrze, ale co jeśli następnego dnia w WTOREK. Jeśli nie uruchomię aplikacji, to jak alarm zostanie przekazany przez Kalendarz.TUESDAY? –

+0

Myślę, że nie dostałeś mojego drugiego punktu, musisz ustawić alarm ponownie w klasie odbiornika, kiedy wywołujesz showNotification (subject); wewnątrz metody onReceive. Nie musisz uruchamiać aplikacji AlarmManager zrobi to za Ciebie. – Chitrang

Powiązane problemy