gpt4 book ai didi

java - 多个警报同时触发

转载 作者:行者123 更新时间:2023-12-02 13:00:33 25 4
gpt4 key购买 nike

friend 们,我有两个问题。第一个问题我设置了多个闹钟,但是当我启动应用程序时,即使不是时间,它们也会同时启动顺便说一句,我在项目中使用对象是我的代码:

主要 fragment :

@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);
}
}

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

提前谢谢您。

最佳答案

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:00 左右唤醒设备以发出警报,并每天同一时间重复一次:

 // 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);

关于java - 多个警报同时触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44321619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com