android 设置只重复一次的闹钟
android set an alarm with only one repeat
目前我在我的 android 应用程序中设置闹钟,如下所示:
scheduleNotification(getNotification("HELLO WORLD"), 5000, 1);
private void scheduleNotification(Notification notification, int delay, int id) {
Intent notificationIntent = new Intent(this, ShowNotification.class);
notificationIntent.putExtra("NOTIFICATION_ID", id);
notificationIntent.putExtra("NOTIFICATION", notification);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(
AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + delay,
PendingIntent.getBroadcast(this, id, notificationIntent, PendingIntent.FLAG_ONE_SHOT));
}
private Notification getNotification(String content) {
Notification.Builder builder = new Notification.Builder(this);
builder.setContentText(content);
builder.setSmallIcon(R.drawable.ic_notification_appicon);
return builder.getNotification();
}
有没有办法设置一个闹钟,闹钟会在 2 小时后激活一次,并且只激活一次?
AlarmManager 中没有设置方法可以做到这一点。你应该有自己的计数器在 N 次后停止它。
第一次触发时可以重新设置闹钟。使用 notificationIntent 操作很简单。
第二次设置闹钟时不应添加新闹钟。
你的意思是警报会响两次?
在特定时间一次,其他时间在 2 小时后?
您可以为第二个闹钟使用不同的 ID。
类似于:
public final static int ID_DEFAULT = 1;
private final static int ID_REPEAT = 2;
...
switch (id) {
case ID_DEFAULT:
scheduleNotification(notification, DELAY_TWO_HOURS, ID_REPEAT);
case ID_REPEAT:
// Your code
break;
}
这将在 2 小时后调用,试试看
PendingIntent pendingIntent = PendingIntent.getBroadcast(activity, notificationId, myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) activity.getSystemService(
activity.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, 7200000 , pendingIntent);
目前我在我的 android 应用程序中设置闹钟,如下所示:
scheduleNotification(getNotification("HELLO WORLD"), 5000, 1);
private void scheduleNotification(Notification notification, int delay, int id) {
Intent notificationIntent = new Intent(this, ShowNotification.class);
notificationIntent.putExtra("NOTIFICATION_ID", id);
notificationIntent.putExtra("NOTIFICATION", notification);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(
AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + delay,
PendingIntent.getBroadcast(this, id, notificationIntent, PendingIntent.FLAG_ONE_SHOT));
}
private Notification getNotification(String content) {
Notification.Builder builder = new Notification.Builder(this);
builder.setContentText(content);
builder.setSmallIcon(R.drawable.ic_notification_appicon);
return builder.getNotification();
}
有没有办法设置一个闹钟,闹钟会在 2 小时后激活一次,并且只激活一次?
AlarmManager 中没有设置方法可以做到这一点。你应该有自己的计数器在 N 次后停止它。
第一次触发时可以重新设置闹钟。使用 notificationIntent 操作很简单。
第二次设置闹钟时不应添加新闹钟。
你的意思是警报会响两次? 在特定时间一次,其他时间在 2 小时后?
您可以为第二个闹钟使用不同的 ID。
类似于:
public final static int ID_DEFAULT = 1;
private final static int ID_REPEAT = 2;
...
switch (id) {
case ID_DEFAULT:
scheduleNotification(notification, DELAY_TWO_HOURS, ID_REPEAT);
case ID_REPEAT:
// Your code
break;
}
这将在 2 小时后调用,试试看
PendingIntent pendingIntent = PendingIntent.getBroadcast(activity, notificationId, myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) activity.getSystemService(
activity.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, 7200000 , pendingIntent);