我希望在工作日设置通知。我在警告对话框中添加了一串天来显示日期。
我想在用户在警告对话框中选择的日历中设置日期。此外,这应该在同一天下周重播。
我已经设置了时间选择器对话框来选择通知时间。但是如果我做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();
}
});
请帮助..
答案 0 :(得分:0)
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));
}
}
答案 1 :(得分:0)
希望您已经解决了您的问题。但是我还是会发布我的解决方案。因此,如果有人绊倒。
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);
}
}
}
long NOTIFICATION_INTERVAL = 7 * 24 * 60 * 60 * 1000;
和int NOTIFICATION_TIME_HOUR = 9;