Android中使用AlarmManager的多个警报?

时间:2019-05-07 11:42:25

标签: android alarmmanager android-alarms

我正在创建一个闹钟应用程序,如何设置多个闹钟?

   SELECT
      id,MIN(st.prince),MAX(st.price)
    FROM CODE_TABLE ct
    INNER JOIN SOLD_TABLE st ON ct.id= st.id

2 个答案:

答案 0 :(得分:1)

setRepeating上使用alarmManager。并且不要忘记在创建挂起的意图时为每个警报设置一个新ID,而不是使用0

  

pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, 0);

第一个0,是警报ID。如果您希望将来将该警报更新为最后一个PendingIntent.FLAG_UPDATE_CURRENT,也请使用0

答案 1 :(得分:1)

您需要为待处理的意图使用不同的广播ID。像这样:

public void startAlarm(int hour, int minute) {
    final int _id = (int) System.currentTimeMillis();
    Toast.makeText(getApplicationContext(), "Starting Alarm", Toast.LENGTH_SHORT).show();
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minute);
    calendar.set(Calendar.SECOND, 0);

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent myIntent = new Intent(MainActivity.this, AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(MainActivity.this, _id, myIntent, PendingIntent.FLAG_ONE_SHOT);
    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, pendingIntent);
}