gpt4 book ai didi

java - 无法显示生日提醒

转载 作者:行者123 更新时间:2023-12-01 20:04:51 24 4
gpt4 key购买 nike

我的应用程序包含一个 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());
}

}

最佳答案

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));

用此替换您的代码。

关于java - 无法显示生日提醒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47586446/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com