如何在Android AlarmManager中安排未来的重复报警?
我能够使用以下代码在Android中设置重复警报。然而,问题是它也会立即发出通知,我不希望这样。我只想让用户输入他们希望闹钟在什么时候响的时间,让闹钟在那个时候响,然后在指定的时间间隔内重复。我尝试切换已运行的实时唤醒而不是RTC唤醒,但是该代码对我不起作用如何在Android AlarmManager中安排未来的重复报警?,android,notifications,alarmmanager,Android,Notifications,Alarmmanager,我能够使用以下代码在Android中设置重复警报。然而,问题是它也会立即发出通知,我不希望这样。我只想让用户输入他们希望闹钟在什么时候响的时间,让闹钟在那个时候响,然后在指定的时间间隔内重复。我尝试切换已运行的实时唤醒而不是RTC唤醒,但是该代码对我不起作用 public static long getTimeForNotification(int hour, int minutes, int am_pm){ Calendar calendar = Calendar.getInstanc
public static long getTimeForNotification(int hour, int minutes, int am_pm){
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR, hour);
calendar.set(Calendar.MINUTE, minutes);
calendar.set(Calendar.AM_PM, am_pm);
return calendar.getTimeInMillis();
}
public static PendingIntent createNotificationPendingIntent(
String name,
String number,
String messageArrayListString,
String contactID,
String photo_uri,
String actionType,
Context mContext
){
Intent alertIntent = new Intent(mContext, AlertReceiver.class);
alertIntent.putExtra("name", name);
alertIntent.putExtra("number", number);
alertIntent.putExtra("messageList", messageArrayListString);
alertIntent.putExtra("contactID", contactID.toString());
alertIntent.putExtra("photo_uri", photo_uri);
alertIntent.setAction(actionType);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, Integer.parseInt(contactID.toString()), alertIntent, PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
}
public static void createNotifications(PendingIntent notificationPendingIntent, Context mContext, Long alarmTime, int frequencyMultiplier){
AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, AlarmManager.INTERVAL_DAY * Long.valueOf(frequencyMultiplier), notificationPendingIntent);
}
提前感谢 这就是我所用的
public class AlarmHelper {
private static final String TAG = "AlarmHelper";
private static final int REQUEST_CODE = 12377;
private static final int REQUEST_CODE_OVERTIME = 12376;
private AlarmHelper() {
}
public static void scheduleAlarm(Context c) {
AlarmManager manager = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(c, REQUEST_CODE, new Intent(c, ClockInBroadcastReceiver.class), PendingIntent.FLAG_CANCEL_CURRENT);
//Set next clock-in time
GregorianCalendar time = new GregorianCalendar();
time.add(...,...); //Whatever you want to add.
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
manager.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis() + 1000, pendingIntent);
} else if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
manager.setExact(AlarmManager.RTC_WAKEUP, time.getTimeInMillis() + 1000, pendingIntent);
} else {
manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time.getTimeInMillis() + 1000, pendingIntent);
}
}
}
公共类AlarmHelper{
私有静态最终字符串标记=“AlarmHelper”;
私有静态最终整数请求_代码=12377;
私有静态最终整数请求\代码\超时=12376;
私人AlarmHelper(){
}
公共静态无效调度报警(上下文c){
AlarmManager=(AlarmManager)c.getSystemService(Context.ALARM\u服务);
PendingEvent PendingEvent=PendingEvent.getBroadcast(c,请求代码,新意图(c,ClockInBroadcastReceiver.class),PendingEvent.FLAG_取消当前);
//及时设置下一个时钟
GregorianCalendar时间=新的GregorianCalendar();
time.add(…,…);//无论你想添加什么。
if(android.os.Build.VERSION.SDK_INT
这对您有帮助吗?