[英]How to set repeated notification for specific day of week?
I am looking for to set a notification on week days. 我要在工作日设置通知。 I have added a string of days in alert dialog to show the days. 我在警报对话框中添加了几天字符串以显示日期。
I want to set the day in calendar which user has selected in the alert dialog. 我想在日历中设置用户在警报对话框中选择的日期。 Also, this should repeat next week on same day. 同样,这应该在下周的同一天重复。
I have put time picker dialog to choose time of notification. 我已经放置了时间选择器对话框来选择通知时间。 But if I do c.getTime() , so I get the current date and time. 但是如果我执行c.getTime(),那么我会得到当前的日期和时间。
How to set notification for other days? 如何设定其他日子的通知? Like if today is monday and I want to create notification on wed? 就像今天是星期一,我想在星期三创建通知吗? How can I set wed in calendar? 如何在日历中设置星期三?
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND,0);
c.set(Calendar.MILLISECOND,0);
notification = c.getTime();
notificationTime = df.format(notification);
notifyTime.setText(notificationTime);
Toast.makeText(getApplicationContext(),String.valueOf(notification),Toast.LENGTH_SHORT).show();
For notification I am using alarm manager. 对于通知,我正在使用警报管理器。
private void setAlarm(Calendar targetmCalen) {
Intent intent = new Intent(getBaseContext(),NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,targetmCalen.getTimeInMillis(),
AlarmManager.INTERVAL_DAY *7, pendingIntent);
ComponentName receiver = new ComponentName(getApplicationContext(),NotificationReceiver.class);
PackageManager pm = getApplicationContext().getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
Toast.makeText(getApplicationContext(),"Notification Set",Toast.LENGTH_SHORT).show();
}
Alert dialog for selecting day: 选择日期的警报对话框:
selectDay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final AlertDialog.Builder builder = new AlertDialog.Builder(AddEventActivity.this);
builder.setSingleChoiceItems(R.array.day_array, -1,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
String[] day = getBaseContext().getResources().getStringArray(R.array.day_array);
selectDay.setText(day[item]);
dialog.dismiss();
}
});
builder.show();
}
});
Do I have to set dayofweek in calendar instance inside alert dialog? 我是否需要在警报对话框中的日历实例中设置“ dayofweek”? Like 喜欢
c.set(Calendar.DAY_OF_WEEK)? c.set(Calendar.DAY_OF_WEEK)?
EDIT : 编辑:
I tried to do this way. 我试图这样做。 But dose this is may be going one day ahead every time if i choose day multiple times. 但是如果我多次选择一天的话,这可能是每次提前一天。
selectDay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final AlertDialog.Builder builder = new AlertDialog.Builder(AddEventActivity.this);
builder.setSingleChoiceItems(R.array.day_array, -1,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
String[] day = getBaseContext().getResources().getStringArray(R.array.day_array);
selectDay.setText(day[item]);
dayOfWeek = day[item];
switch (dayOfWeek)
{
case "Mon":
c.set(Calendar.DAY_OF_WEEK,2);
c.getTime();
Toast.makeText(getApplicationContext(),String.valueOf(c.getTime()),Toast.LENGTH_SHORT).show();
break;
case "Tue":
c.set(Calendar.DAY_OF_WEEK, 3);
c.getTime();
Toast.makeText(getApplicationContext(),String.valueOf(c.getTime()),Toast.LENGTH_SHORT).show();
break;
case "Wed":
c.set(Calendar.DAY_OF_WEEK, 4);
c.getTime();
Toast.makeText(getApplicationContext(),String.valueOf(c.getTime()),Toast.LENGTH_SHORT).show();
break;
case "Thu":
c.set(Calendar.DAY_OF_WEEK, 5);
c.getTime();
Toast.makeText(getApplicationContext(),String.valueOf(c.getTime()),Toast.LENGTH_SHORT).show();
break;
case "Fri":
c.set(Calendar.DAY_OF_WEEK, 6);
c.getTime();
Toast.makeText(getApplicationContext(),String.valueOf(c.getTime()),Toast.LENGTH_SHORT).show();
break;
case "Sat":
c.set(Calendar.DAY_OF_WEEK, 7);
c.getTime();
Toast.makeText(getApplicationContext(),String.valueOf(c.getTime()),Toast.LENGTH_SHORT).show();
break;
case "Sun":
c.set(Calendar.DAY_OF_WEEK,1);
c.getTime();
Toast.makeText(getApplicationContext(),String.valueOf(c.getTime()),Toast.LENGTH_SHORT).show();
}
dialog.dismiss();
}
});
builder.show();
}
});
Please help.. 请帮忙..
public class TimerReceiver extends BroadcastReceiver {
public void scheduleAlarms(Context paramContext) {
Calendar calendar = Calendar.getInstance();
if (strmyTime.contains("One Week")) {
calendar.set(Calendar.DATE, 7);
registerReceiver(paramContext, calendar, AlarmManager.INTERVAL_DAY * 7);
}
}
public void registerReceiver(Context paramContext, Calendar calender, long interval) {
AlarmManager localAlarmManager = (AlarmManager) paramContext.getSystemService(Context.ALARM_SERVICE);
PendingIntent localPendingIntent = PendingIntent.getService(paramContext, 0,
new Intent(paramContext, NotificationService.class), PendingIntent.FLAG_UPDATE_CURRENT);
localAlarmManager.setRepeating(AlarmManager.RTC, calender.getTimeInMillis(), interval, localPendingIntent);
}
@Override
public void onReceive(Context context, Intent intent) {
scheduleAlarms(context);
context.startService(new Intent(context, Notification.class));
}
} }
I hope you already have solved your problem. 希望您已经解决了您的问题。 But I will post my solution nonetheless. 但是我还是会发布我的解决方案。 So if anyone stumps upon. 因此,如果有人绊倒。
private void sendNotification(){
Intent notificationIntent = new Intent(HomeActivity.this, NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
NOTIFICATION_REQUEST_CODE,
notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, CONST.NOTIFICATION_TIME_HOUR);
calendar.set(Calendar.MINUTE, 0);
for (int i = 1; i < 8; ++i){
if (i != 6){
calendar.set(Calendar.DAY_OF_WEEK, i);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), CONST.NOTIFICATION_INTERVAL, pendingIntent);
}
}
}
Here long NOTIFICATION_INTERVAL = 7 * 24 * 60 * 60 * 1000;
这里long NOTIFICATION_INTERVAL = 7 * 24 * 60 * 60 * 1000;
& int NOTIFICATION_TIME_HOUR = 9;
& int NOTIFICATION_TIME_HOUR = 9;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.