我正在构建一个警报应用程序。我已经成功实现了基本的报警功能。
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.HOUR_OF_DAY, sHour);
calendar.set(calendar.MINUTE, sMin);
calendar.set(calendar.SECOND, 0);
calendar.set(calendar.MILLISECOND, 0);
long sdl = calendar.getTimeInMillis();
Intent intent = new Intent(AlarmList.this, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(AlarmList.this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager ALARM1 = (AlarmManager)getSystemService(ALARM_SERVICE);
ALARM1.set(AlarmManager.RTC_WAKEUP, sdl, sender);
在我的应用程序中,用户可以选择天(sunday,monday...)
每周重复闹钟。
我正在尝试创建多个警报,每周重复一次,但不知道该怎么做。
我可以使用(重复)间隔创建它还是应该创建多个警报管理器?
答案 0 :(得分:142)
您需要different Broadcast id's
使用pending intents
。就像是
这样:
Intent intent = new Intent(load.this, AlarmReceiver.class);
final int _id = (int) System.currentTimeMillis();
PendingIntent appIntent = PendingIntent.getBroadcast(this, _id, intent,PendingIntent.FLAG_ONE_SHOT);
使用系统时间应该是每个待处理的唯一标识符 意图你开火。
答案 1 :(得分:10)
来自the docs:
如果已安排此Intent的警报(使用 那么,filterEquals(Intent)定义了两个意图的相等性 它将被移除并由此替换
多个AlarmManagers
无法解决您的问题。如果它们有多个不同的警报(不同的时间和不同的天数),那么每次启动上一个警报时,您需要在BroadcastReceiver
内设置警报。
您还需要按住RECEIVE_BOOT_COMPLETED
并拥有BroadcastReceiver
来接收启动,这样如果手机重新启动,您可以重新安排闹钟。
答案 2 :(得分:6)
要设置多个闹钟,您需要每次定义Intent
,以便与其他闹钟区分开来。我发现这样做的最简单方法是设置data
内容的Intent
字段,如下所示:
// give your alarm an id and save it somewhere
// in case you want to cancel it in future
String myAlarmId = ...;
// create your Intent
Intent intent = new Intent(AlarmList.this, AlarmReceiver.class);
intent.setData(Uri.parse("myalarms://" + myAlarmId));
...
你的代码@Hassy31的其余部分都很好并保持不变。
请注意,根据文档,requestCode
方法中的PendingIntent.getBroadcast()
参数(由@parag建议)未使用,因此这不是正确的方法。
答案 3 :(得分:2)
为pendingIntent设置广播id
for (int id = 0; id < 3; id++) {
// Define pendingintent
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id,ntent, 0);
// set() schedules an alarm
alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, pendingIntent);
}
答案 4 :(得分:1)
使用Android警报管理器设置多个警报
//RC_ARRAY is store all the code that generate when alarm is set
private lateinit var RC_ARRAY:ArrayList<Int>
//tick is just hold the request when new alarm set
private var tick :Int=0
//setAlarm method set alarm
fun setAlarm(c: Calendar, context: Context) {
val manager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
//when alarm store set the request assign to tick variable
tick = System.currentTimeMillis().toInt()
//Add all the alarm Request into RC_ARRAY for just cancel the alarm
RC_ARRAY.add(tick)
//Notification Broadcast intent
val intentAlarm = Intent(context, AlaramFireReceiver::class.java).let {
PendingIntent.getBroadcast(context, tick, it, PendingIntent.FLAG_ONE_SHOT)
}
//alarm fire next day if this condition is not statisfied
if (c.before(Calendar.getInstance())) {
c.add(Calendar.DATE, 1)
}
//set alarm
manager.setExact(AlarmManager.RTC_WAKEUP, c.timeInMillis, intentAlarm)
}
//remove specific alarm
private fun removeAlarm(context: Context) {
val manager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
//remove specific alarm according to alarm request code
for (i in RC_ARRAY){
val intentAlarm = Intent(context, AlaramFireReceiver::class.java).let {
PendingIntent.getBroadcast(context, i, it, 0)
}
//cancel alarm
manager.cancel(intentAlarm)
}
}
//delivers notification for alarm
class AlaramFireReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
//Notification ID
val channelid="channelId"
val manger=context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
//check for device only available for Oreo and above
if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.O){
val channel= NotificationChannel(channelid,"alarm notification",NotificationManager.IMPORTANCE_HIGH)
channel.enableLights(true)
manger.createNotificationChannel(channel)
}
//build notification
val build=NotificationCompat.Builder(context,channelid)
.setSmallIcon(R.drawable.ic_access_time_black_24dp)
.setContentTitle("alarm")
.setContentTitle("time done")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setColor(Color.RED)
//Deliver notification
manger.notify(0,build.build())
}
}
答案 5 :(得分:0)
我要做的事情类似于您如何移至链表中的下一个元素。我在数据库中保留了一个ReminderEntity,该实体具有用户启用警报的一周中所有时间的警报。 然后我只安排第一天。当第一天触发时,那一刻我安排第二天,依此类推。 如果用户在发生第一个警报之前将其删除,则同样。在这种情况下,我将从实体中清除已删除的日期,并为下一个可用的时间安排警报。