【发布时间】:2016-02-06 13:08:24
【问题描述】:
我正在寻找在工作日设置通知。我在警报对话框中添加了一串天来显示天数。
我想设置用户在警报对话框中选择的日历中的日期。此外,这应该在下周的同一天重复。
我已放置时间选择器对话框来选择通知时间。但是如果我这样做 c.getTime() ,那么我会得到当前的日期和时间。
如何设置其他日子的通知?就像今天是星期一,我想在星期三创建通知?如何在日历中设置星期三?
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();
对于通知,我正在使用警报管理器。
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();
}
选择日期的警报对话框:
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();
}
});
我必须在警报对话框内的日历实例中设置 dayofweek 吗?喜欢
c.set(Calendar.DAY_OF_WEEK)?
编辑:
我试图这样做。但是如果我多次选择一天,这可能每次都会提前一天。
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();
}
});
请帮忙..
【问题讨论】:
-
将通知放入
service -
我已经完成了。我收到通知,但不是在所需的日期。我该如何设置日期?
-
你总是可以创建一个 Date d 对象,然后 c.setTime(d)
-
请问你能用代码告诉我吗?请检查编辑。
-
我不确定您是否有当前日期,但如果有,请在现在时间创建一个日期对象并将其天数加 7(因为您希望它在同一天)然后您可以c.setTime(日期对象)
标签: java android push-notification repeat dayofweek