多个警报同时触发

时间:2017-06-02 05:44:48

标签: java android alarmmanager

我的朋友有两个问题。 第一个问题我设置了多个警报,但是当我启动应用程序时,它们都会在同一时间启动,即使不是时候 顺便说一下,我在项目中使用对象是我的代码:

主要片段:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    final View view = inflater.inflate(R.layout.fragment_liste_alarme, container, false);
    final ProgrammeBDD programmeBDD = new ProgrammeBDD(getActivity());
    lvProgrammes = (ListView) view.findViewById(R.id.lvprogrammes);
    programmeBDD.open();
    //programmeBDD.insertProgramme(new Programme("Medii1", "03:15", "0111000", 1, 1, 1));

    programmeList = programmeBDD.getAllProgramme();
    Collections.sort(programmeList);
    adapter = new CustomList(getActivity(), programmeList);
    lvProgrammes.setAdapter(adapter);
    setAlarm();
    programmeBDD.close();
    return view;
}

setAlarm()方法:

private void setAlarm() {
    alarmManager = new AlarmManager[programmeList.size()];
    intentArray = new ArrayList<PendingIntent>();
    for (int i = 0; i < programmeList.size(); i++) {

        Intent intent = new Intent(getActivity(), AlarmReceiver.class);
        intent.putExtra("IdProgramme", programmeList.get(i).getId());
        PendingIntent pi = PendingIntent.getBroadcast(getActivity(), i, intent, 0);
        alarmManager[i] = (AlarmManager) getActivity().getSystemService(ALARM_SERVICE);
        alarmManager[i].set(AlarmManager.RTC_WAKEUP, toCalendar(programmeList.get(i).getDateTime()).getTimeInMillis(), pi);
        intentArray.add(pi);
    }
}

我的接收者课程:

public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    Toast.makeText(context, "Your Time is up!!!!!", Toast.LENGTH_LONG).show();
    Vibrator vib=(Vibrator)context.getSystemService(context.VIBRATOR_SERVICE);    //for Vibration
    vib.vibrate(2000);

    Intent i=new Intent(context,AlarmReceiverActivity.class);  //song class contain media song
    int id = intent.getIntExtra("IdProgramme",0);
    i.putExtra("IdProgramme", id);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);
}
}

第二个问题是,我发出的每一个警报都会在一段特定的不同时间后重复。

提前谢谢。

1 个答案:

答案 0 :(得分:1)

ELAPSED_REALTIME_WAKEUP示例 以下是使用ELAPSED_REALTIME_WAKEUP的一些示例。

唤醒设备以在30分钟内发出警报,之后每30分钟发出警报:

// Hopefully your alarm will have a lower frequency than this!
alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
    SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_HALF_HOUR,
    AlarmManager.INTERVAL_HALF_HOUR, alarmIntent);

唤醒设备以在一分钟内发出一次性(非重复)警报:

  private AlarmManager alarmMgr;
  private PendingIntent alarmIntent;
  ...
  alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
  Intent intent = new Intent(context, AlarmReceiver.class);
  alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

  alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
    SystemClock.elapsedRealtime() +
    60 * 1000, alarmIntent);

RTC示例 以下是使用RTC_WAKEUP的一些示例。

唤醒设备以在下午2点左右发出警报,并同时每天重复一次:

 // Set the alarm to start at approximately 2:00 p.m.
 Calendar calendar = Calendar.getInstance();
 calendar.setTimeInMillis(System.currentTimeMillis());
 calendar.set(Calendar.HOUR_OF_DAY, 14);

 // With setInexactRepeating(), you have to use one of the AlarmManager interval
 // constants--in this case, AlarmManager.INTERVAL_DAY.
 alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,          calendar.getTimeInMillis(),
    AlarmManager.INTERVAL_DAY, alarmIntent);

唤醒设备,准时上午8:30准时发出警报,此后每20分钟发出一次警报:

  private AlarmManager alarmMgr;
  private PendingIntent alarmIntent;
  ...
  alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
  Intent intent = new Intent(context, AlarmReceiver.class);
  alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

  // Set the alarm to start at 8:30 a.m.
  Calendar calendar = Calendar.getInstance();
  calendar.setTimeInMillis(System.currentTimeMillis());
  calendar.set(Calendar.HOUR_OF_DAY, 8);
  calendar.set(Calendar.MINUTE, 30);

  // setRepeating() lets you specify a precise custom interval--in this case,
 // 20 minutes.
 alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
    1000 * 60 * 20, alarmIntent);