更新时间:2022-05-23 21:46:17
你应该设置两个报警管理器。对于第一个设置的触发时间通过计算适当时间的剩余时间(例如上午8:00)。然后你应该在你的第一个警报管理器内创建另一个警报管理器,每个触发15分钟。
you should setup two alarm manager.for the first one set trigger time by calculating the remain time to your appropriate time (for example 8:00 Am). and after that you should create another alarm manager inside of your first alarm manager that will trigger each 15 minutes.
用于设置第一个警报管理器的计算时间使用下面的代码:
for calculate time for set first alarm manager use the code bellow:
Calendar c = Calendar.getInstance();
int m = calendar.get(Calendar.MINUTE);
long start = System.currentTimeMillis();
int remain=0;
if (m<15)
{
remain = 15-m;
}
else if (m<30)
{
remain = 30-m;
}
else if (m<45)
{
remain = 45-m;
}
else
{
remain = 60-m;
}
remain=start + remain*60*1000// convert it to milisecond and plus it to current time;
修改
之后你可以使用下面的代码设置第一个报警管理器:
and after that you can use code bellow to setup first alarm manager:
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, FirstReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
am.set(AlarmManager.RTC_WAKEUP,remain, pi);
所以现在在你的FirstReceiver课上做类似下面的事情:
so now in your FirstReceiver class do something like bellow:
public class FirstReceiver extends BroadcastReceiver
{
//do what ever you want + code bellow to setup second alarm manager
Intent intent = new Intent(this, SecAlarm.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager am= (AlarmManager)getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME,SystemClock.elapsedRealtime(),
15*60*60,pendingIntent);
}