【发布时间】:2026-01-30 21:20:04
【问题描述】:
我有一个活动,用户选择日期和时间,并将日期输入到 sqlite 数据库中。从活动中,我正在启动如下所示的 IntentService
Intent intents = new Intent (this,AlarmIntentService.class);
startService(intents);
在intentservice中,我将sqlite数据库中的这些数据与当前系统时间进行比较,并广播警报。
当我第一次调用意图服务时,它工作正常,并且有一个条目进入数据库。但是,当用户将第二个数据插入 sqlite 数据库并启动 Intentservice 时,它不起作用。
是否有用新数据更新传递的意图服务?
intentservice onhandleintent 描述如下
final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
final SqliteController db = new SqliteController(getApplicationContext());
List<Greetings> greetings = db.getAllGreetings();
int k = db.getGreetingsCount();
String p = String.valueOf(k);
Log.d("p", p);
if (db.getGreetingsCount() >= 0) {
do {
for (Greetings g : greetings) {
Calendar cal = Calendar.getInstance();
d1 = cal.get(Calendar.DAY_OF_MONTH);
d2 = cal.get(Calendar.MONTH);
d3 = cal.get(Calendar.YEAR);
t1 = cal.get(Calendar.HOUR);
t2 = cal.get(Calendar.MINUTE);
t3 = cal.get(Calendar.AM_PM);
if (t3 == 1) {
t4 = "PM";
} else {
t4 = "AM";
}
tc = String.valueOf(t1) + " : " + t2 + " " + t4;
d = String.valueOf(d3) + "-" + String.valueOf(d2 + 1) + "-" + String.valueOf(d1);
cdt = d + "\t" + tc;
time = g.getTime();
date = g.getDate();
phone = g.getPhoneNumber();
msg = g.getMessage();
id = g.getID();
dnt = date + "\t" + time;
if (dnt.equals(cdt)) {
Intent aint = new Intent(getApplicationContext(), AlarmReceiver.class);
aint.putExtra("msg", msg);
aint.putExtra("phone", phone);
aint.putExtra("id", id);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, aint, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
showNotification();
db.deleteGreetings(g);
}
}
k = db.getGreetingsCount();
} while (k != 0);
【问题讨论】:
标签: android sqlite intentservice