我是android新手,目前正在研究Notifications。
我有两种要显示的通知。一个是每周提醒,另一个是每月提醒。
这是我设置每周提醒的方式:
manager.setRepeating(AlarmManager.RTC_WAKEUP, currentTime, AlarmManager.INTERVAL_DAY *7, pendingIntent);
这是我设置每月提醒的方法:
long monthlyDuration = getDuration();
manager.setRepeating(AlarmManager.RTC_WAKEUP, currentTime, monthlyDuration, pendingIntent);
private long getDuration(){
// get todays date
Calendar cal = Calendar.getInstance();
// get current month
int currentMonth = cal.get(Calendar.MONTH);
// move month ahead
currentMonth++;
// check if has not exceeded threshold of december
if(currentMonth > Calendar.DECEMBER){
// alright, reset month to jan and forward year by 1 e.g fro 2013 to 2014
currentMonth = Calendar.JANUARY;
// Move year ahead as well
cal.set(Calendar.YEAR, cal.get(Calendar.YEAR)+1);
}
// reset calendar to next month
cal.set(Calendar.MONTH, currentMonth);
// get the maximum possible days in this month
int maximumDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH, maximumDay);
long thenTime = cal.getTimeInMillis();
return (thenTime);
}
这是通知代码:
int importance = NotificationManager.IMPORTANCE_HIGH;
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "default");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = context.getString(R.string.channel_name);
String description = context.getString(R.string.col_week_rem_description);
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
notificationManager.createNotificationChannel(channel);
}
builder.setAutoCancel(false);
builder.setChannelId(CHANNEL_ID);
builder.setContentTitle(context.getResources().getString(R.string.app_name));
builder.setContentText(context.getResources().getString(R.string.notification_text));
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.setSmallIcon(R.drawable.ic_launcher_notification);
int myColor =
context.getResources().getColor(R.color.notification_icon_bg);
builder.setColor(myColor);
}
else {
builder.setSmallIcon(R.drawable.ic_launcher);
}
builder.setContentIntent(pendingIntent);
// builder.setOngoing(true);
// builder.setNumber(100);
notificationManager.notify(NOTIFICATION_ID,builder.build());
}
我面临的问题是,从设置显示提醒的选项开始,通知在一分钟或两分钟内弹出。我想做的是,当用户选择每周提醒时,他/她应该在7天后的同一时间收到通知。也就是说,如果我今天选择每周三提醒。然后,从用户选择每周提醒之日起7天后,我应该在3点钟收到通知。每月提醒也以相同的方式起作用。
我在这里和其他地方经历了很多线程,但是无法解决问题。谁能帮我解决问题。
谢谢。
答案 0 :(得分:0)
public static void setReminder(Context ctx) {
Calendar setcalendar = Calendar.getInstance();
// setcalendar.set(Calendar.DAY_OF_WEEK, 3);
setcalendar.set(Calendar.HOUR_OF_DAY, //notification hr);
setcalendar.set(Calendar.MINUTE, //notification min.);
setcalendar.set(Calendar.SECOND, //notification sec.);
setcalendar.set(Calendar.DAY_OF_WEEK, //notification day);
Intent intent = new Intent(ctx, NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) ctx.getSystemService(Activity.ALARM_SERVICE);
long timeMs = setcalendar.getTimeInMillis();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeMs, pendingIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
am.setExact(AlarmManager.RTC_WAKEUP, timeMs, pendingIntent);
} else {
am.set(AlarmManager.RTC_WAKEUP, timeMs, pendingIntent);
}
Log.e("", "setReminder: reminder has been set");
}