2017-01-23 2 views
0

Я смог установить повторяющийся сигнал тревоги в Android, используя следующий код. Однако проблема заключается в том, что она также делает немедленные уведомления, и я не хочу этого. Я просто хочу, чтобы пользователь установил время, в течение которого они хотят, чтобы будильник погас, и в это время будильник погаснет и повторится с заданным интервалом, который был выбран. Я попытался переключить ELAPSED_REALTIME_WAKEUP вместо RTC_WAKEUP, но этот код не работал для меня.Как запланировать будущую повторяющуюся тревогу в Android AlarmManager?

public static long getTimeForNotification(int hour, int minutes, int am_pm){ 
    Calendar calendar = Calendar.getInstance(); 
    calendar.set(Calendar.HOUR, hour); 
    calendar.set(Calendar.MINUTE, minutes); 
    calendar.set(Calendar.AM_PM, am_pm); 
    return calendar.getTimeInMillis(); 
} 

public static PendingIntent createNotificationPendingIntent(
     String name, 
     String number, 
     String messageArrayListString, 
     String contactID, 
     String photo_uri, 
     String actionType, 
     Context mContext 
     ){ 

    Intent alertIntent = new Intent(mContext, AlertReceiver.class); 
    alertIntent.putExtra("name", name); 
    alertIntent.putExtra("number", number); 
    alertIntent.putExtra("messageList", messageArrayListString); 
    alertIntent.putExtra("contactID", contactID.toString()); 
    alertIntent.putExtra("photo_uri", photo_uri); 
    alertIntent.setAction(actionType); 


    PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, Integer.parseInt(contactID.toString()), alertIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

    return pendingIntent; 
} 

public static void createNotifications(PendingIntent notificationPendingIntent, Context mContext, Long alarmTime, int frequencyMultiplier){ 

    AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, AlarmManager.INTERVAL_DAY * Long.valueOf(frequencyMultiplier), notificationPendingIntent); 

} 

Заранее благодарен!

ответ

0

Это то, что я использую.

public class AlarmHelper { 
    private static final String TAG = "AlarmHelper"; 
    private static final int REQUEST_CODE = 12377; 
    private static final int REQUEST_CODE_OVERTIME = 12376; 

    private AlarmHelper() { 
    } 

    public static void scheduleAlarm(Context c) { 
     AlarmManager manager = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(c, REQUEST_CODE, new Intent(c, ClockInBroadcastReceiver.class), PendingIntent.FLAG_CANCEL_CURRENT); 


     //Set next clock-in time 
     GregorianCalendar time = new GregorianCalendar(); 
     time.add(...,...); //Whatever you want to add. 


     if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { 
      manager.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis() + 1000, pendingIntent); 
     } else if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { 
      manager.setExact(AlarmManager.RTC_WAKEUP, time.getTimeInMillis() + 1000, pendingIntent); 
     } else { 
      manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time.getTimeInMillis() + 1000, pendingIntent); 
     } 

    } 

} 
+0

Помогло ли это вам? –

Смежные вопросы