用户可以使用datepicker选择日期(目标日期)。日期将保存在数据库中。然后,只有截止日期在2天内,才会在屏幕上弹出通知。我想弄清楚如何根据所选日期每日/每周/每月显示通知。我检查了不同的网站,但没有得到适当的解决方案。请帮忙!
MainActivity
swipelist.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(final int position, SwipeMenu menu, int index) {
// ApplicationInfo item = mAppList.get(position);
final MyImage image = (MyImage) swipelist.getItemAtPosition((int) position);
switch (index) {
case 0:
// open
LayoutInflater li = LayoutInflater.from(MainActivity.this);
View DateView = li.inflate(R.layout.calendar_cam, null);
build = new AlertDialog.Builder(MainActivity.this);
build.setTitle("Reminder");
build.setMessage("Pick a date");
build.setView(DateView);
newDate = (Button) DateView.findViewById(R.id.buttonCalCam); //Button which opens the calender
newDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
final Calendar c = Calendar.getInstance();
currentYear = c.get(Calendar.YEAR);
currentMonth = c.get(Calendar.MONTH);
currentDay = c.get(Calendar.DAY_OF_MONTH);
build.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
database = dbHelper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(DBhelper.COLUMN_DESCRIPTION, dateString);
Log.d("Updating Date: ", ".....");
String whereClause =
DBhelper.COLUMN_TITLE + "=? AND " + DBhelper.COLUMN_DATETIME + "=?";
String[] whereArgs = new String[]{image.getTitle(), String.valueOf(image.getDatetimeLong())};
database.update(DBhelper.TABLE_NAME, cv, whereClause, whereArgs);
Log.d("Updating Date: ", ".....");
image.setDescription(dateString);
swipelist.invalidateViews();
//Scheduling Notifications
int curYear = c.get(Calendar.YEAR), curMonth = c.get(Calendar.MONTH) + 1, curDay = c.get(Calendar.DAY_OF_MONTH);
int goalYear = Integer.parseInt(yearG), goalMonth = Integer.parseInt(monthG), goalDay = Integer.parseInt(dayG);
int calYear = 0, calMonth = 0, calDay = 0, calDayGoal = 0;
//Get current date
String curDate = String.valueOf(curDay) + "/" + String.valueOf(curMonth) + "/" + String.valueOf(curYear);
//Get goal date
String goalDate = String.valueOf(goalDay) + "/" + String.valueOf(goalMonth) + "/" + String.valueOf(goalYear);
int count = -1;
//Fetches the date and Time from system, hence not used
if (curYear < goalYear || (goalYear == curYear && goalMonth > curMonth) || (goalYear == curYear && goalMonth == curMonth && goalDay > curDay)) {
count = 0;
int i;
for (i = curYear; i < goalYear; i++) {
if (i % 4 == 0) {
count += 366;//Leap year
} else {
count += 365;// Non leap year
}
}
//calculating the no of days left from current date to goal date
count -= calMonthDay(curMonth, curYear);
count -= curDay;
count += calMonthDay(goalMonth, goalYear);
count += goalDay;
if (count < 0) {
count *= -1;
}
if (count != 1) {
daysLeftGoal.add(String.valueOf(count) + " days left");
Toast.makeText(getBaseContext(), "Days Left: "+count, Toast.LENGTH_LONG).show();
}else
daysLeftGoal.add(String.valueOf(count) + " day left");
} else {// current year exceeds goal year
daysLeftGoal.add("Times up");
}
mContext=getApplicationContext();
// check if goal date is less than or equals 2 days
if (count <= 2 && count >= 0) {
Intent resultIntent = new Intent(mContext, MainActivity.class);
//resultIntent.putExtra("ID", mCursor.getString(mCursor.getColumnIndex(DbHelperGoal.KEY_ID)));
resultIntent.putExtra("update", true);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
//TaskStackBuilder requires API 16 [4.1] min
if (Build.VERSION.SDK_INT > 15) {
TaskStackBuilder stackBuilder = TaskStackBuilder.create(MainActivity.this);
//stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
} else {
resultPendingIntent = PendingIntent.getActivity(MainActivity.this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//deletePendingIntent = PendingIntent.getActivity(MainActivity.this, 0, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}
gBuilder = new NotificationCompat.Builder(MainActivity.this).setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("My Goals")
.setContentText("Reminder Set: " + image.getDescription())
.addAction(R.drawable.ic_action_about, "View", resultPendingIntent);
//.addAction(R.drawable.ic_action_discard, "Delete", deletePendingIntent);
gBuilder.setContentIntent(resultPendingIntent);
//gBuilder.setContentIntent(deletePendingIntent);
gBuilder.setPriority(2);// [-2,2]->[PRIORITY_MIN,PRIORITY_MAX]
//for the sound and float notification
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 14);//set the alarm time
calendar.set(Calendar.MINUTE, 57);
calendar.set(Calendar.SECOND, 0);
gBuilder.setWhen(System.currentTimeMillis())
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000}).setLights(Color.WHITE, 0, 1);
//gBuilder.setWhen(calendar.getTimeInMillis()).setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE).setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)).setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 }).setLights(Color.WHITE, 0, 1);
// opens the resultPendingIntent
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
// open the activity every 24 hours
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60 * 1000, resultPendingIntent);
gBuilder.setAutoCancel(true);
//int mNotificationId = mCursor.getInt(mCursor.getColumnIndex(DbHelperGoal.KEY_ID));
//keyIndex = mCursor.getInt(mCursor.getColumnIndex(DbHelperGoal.KEY_ID));
int mNotificationId=position;
// Gets an instance of the NotificationManager service
myGoalNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
myGoalNotifyMgr.notify(mNotificationId, gBuilder.build());
}
}
});
alert=build.create();
alert.show();
break;
//计算几个月的天数
int calMonthDay(int m,int y){//calMonthDay(month,year)
int x=0,c;
for(c = 1; c < m; c++) {// Jan to less than the month 'm' as 'm' we are not taking the the whole days of that month
if(c == 2) {//if Feb
if(y%4 == 0)//checks if year is leap or not
x += 29;
else
x += 28;
}
else
x += mon.get(c-1);
}
return(x);
}
//计算不。从当月起的几个月年到目标月&amp;年
int calDateMonth(int mC,int yC,int mG,int yG){//(current-month, current-year, goal-month, goal-year)
int x = 0,i,countM=0;
if(yC<=yG){
for(i = yC; i < yG; i++)
countM += 12;
}
countM -= mC;
countM += mG;
return (countM);
}
//计算不。从当月开始的几周年到目标月&amp;年
int calDateWeek(int mC,int yC,int mG,int yG){
int x = 0,i,countW=0;
if(yC<=yG){
for(i = yC; i < yG; i++)
countW+=52;
}
countW -= mC;
countW += mG;
countW *= 4;
return (countW);
}
的DatePicker
private DatePickerDialog.OnDateSetListener reservationDate = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day){
final Calendar c = Calendar.getInstance();
Date convertedDate = new Date();
int curYear = c.get(Calendar.YEAR), curMonth = c.get(Calendar.MONTH)+1, curDay = c.get(Calendar.DAY_OF_MONTH);
//Picks the selected date, month & year & displays on button
if((year>curYear)||(year==curYear && month+1>curMonth)||(year==curYear && month+1==curMonth && day>curDay)) {
dayG = Integer.toString(day);
monthG = Integer.toString(month + 1);
yearG = Integer.toString(year);
Log.d("Setting Date: ", ".....");
dateString=String.valueOf(dayG)+"-"+String.valueOf(monthG)+"-"+String.valueOf(yearG);
Toast.makeText(getBaseContext(), "Your reminder is set to " + day + "-" + (month + 1) + "-" + year + ".", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getBaseContext(), "Please choose date after " + curDay + "-" + curMonth + "-" + curYear, Toast.LENGTH_SHORT).show();
}
}
};