我的应用程序包含一个用于存储人员信息的SQLite数据库。 我将生日存储为长值。我希望在保存的列表中显示有人今天过生日的通知。我正在检索长生日值,将其设置为日历并将其与当前日历月和日进行比较。但问题是,如果某人的生日与当天相符,则通知会出现,但表明它是列表中最后一个人的生日,无论是最后一个人的生日。请帮忙。提前谢谢。
以下是代码
calendar_now = Calendar.getInstance();
calendar_now.setTimeInMillis(System.currentTimeMillis());
int month_now = calendar_now.get(Calendar.MONTH);
int day_now = calendar_now.get(Calendar.DAY_OF_MONTH);
//Set a specific time to the calendar
calendar_now.set(Calendar.HOUR_OF_DAY, 6);
calendar_now.set(Calendar.MINUTE, 0);
calendar_now.set(Calendar.SECOND, 0);
dbHelper = new DbHelper(this);
personArrayList = dbHelper.getPerson();
birth_calendar = Calendar.getInstance();
for (Person p : personArrayList) {
name = p.getName(); //Get the person's name
mobile = p.getMobile();
byte_image = p.getByteImage();
position = personArrayList.indexOf(p);
DoB = p.getDateOfBirth();
birth_calendar.setTimeInMillis(DoB);
int birth_month = birth_calendar.get(Calendar.MONTH);
int birthday = birth_calendar.get(Calendar.DAY_OF_MONTH);
if ((birth_month == month_now) && (birthday == day_now)) {
Intent intent = new Intent(this, NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 100, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar_now.getTimeInMillis(), pendingIntent);
} else {
Toast.makeText(this, "No birthday today", Toast.LENGTH_SHORT).show();
}
birth_calendar.clear();
}
NotificationReceiver.java
public class NotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
//Open PersonDetailsActivity on notification click
Intent resultIntent = new Intent(context, PersonDetailsActivity.class);
//Pass the values through intent to PersonDetailsActivity
resultIntent.putExtra("name", MainActivity.name);
resultIntent.putExtra("mobile", MainActivity.mobile);
resultIntent.putExtra("DoB", MainActivity.DoB);
resultIntent.putExtra("image", MainActivity.byte_image);
resultIntent.putExtra("position", MainActivity.position);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 100,
resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = (NotificationCompat.Builder)
new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("Birthday reminder")
.setContentText("It is " +MainActivity.name+ "'s birthday today, " +
"wish him/her \"Happy birthday!!\"")
.setVibrate(new long[]{250, 250, 250, 250})
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setAutoCancel(true);
notificationManager.notify(100, mBuilder.build());
}
}
答案 0 :(得分:3)
if ((birth_month == month_now) && (birthday == day_now)) {
Intent intent = new Intent(this, NotificationReceiver.class);
Intent intent = new Intent(context , DocumentSubFolder.class);
intent.putExtra("name" , name);
intent.putExtra("mobile" , mobile);
intent.putExtra("Dob" , DoB);
intent.putExtra("image" , byte_image);
intent.putExtra("position" , position);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 100, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar_now.getTimeInMillis(), pendingIntent);
}
NotificationReceiver.java
resultIntent.putExtra("name", intent.getStringExtra("name));
resultIntent.putExtra("mobile", intent.getStringExtra("mobile));
resultIntent.putExtra("DoB", intent.getStringExtra("Dob));
resultIntent.putExtra("image", intent.getStringExtra("image));
resultIntent.putExtra("position", intent.getStringExtra("position));
用此替换您的代码。