消息来源代码:
private void saveState() {
DatabaseHelper myDbHelper = new DatabaseHelper(ReminderEditActivity.this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
c=myDbHelper.query("tblmain", null, null, null, null,null, null);
if(c.moveToFirst())
{
do {
mRowId = c.getLong(0);
String datetime = c.getString(8);
SimpleDateFormat dateTimeFormat = new SimpleDateFormat(DATE_TIME_FORMAT);
Date date = null;
try {
date = dateTimeFormat.parse(datetime);
mCalendar.setTime(date);
} catch (ParseException e) {
Log.e("ReminderEditActivity", e.getMessage(), e);
}
mCalendar.setTime(date);
new ReminderManager(this).setReminder(mRowId, mCalendar);
} while (c.moveToNext());
}
}
ReminderManager
public class ReminderManager {
private Context mContext;
private AlarmManager mAlarmManager;
public ReminderManager(Context context) {
mContext = context;
mAlarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
}
public void setReminder(Long taskId, Calendar when) {
Intent i = new Intent(mContext, OnAlarmReceiver.class);
i.putExtra(RemindersDbAdapter.KEY_eventid, (long)taskId);
PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, i, PendingIntent.FLAG_ONE_SHOT);
mAlarmManager.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pi);
}
}
onalarmreceiver
public class OnAlarmReceiver extends BroadcastReceiver {
private static final String TAG = ComponentInfo.class.getCanonicalName();
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Received wake up from alarm manager.");
long rowid = intent.getExtras().getLong(RemindersDbAdapter.KEY_eventid);
WakeReminderIntentService.acquireStaticLock(context);
Intent i = new Intent(context, ReminderService.class);
i.putExtra(RemindersDbAdapter.KEY_eventid, rowid);
context.startService(i);
}
}
ReminderService
public class ReminderService extends WakeReminderIntentService {
public ReminderService() {
super("ReminderService");
}
@Override
void doReminderWork(Intent intent) {
Log.d("ReminderService", "Doing work.");
Long rowId = intent.getExtras().getLong(RemindersDbAdapter.KEY_eventid);
NotificationManager mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, ReminderEditActivity.class);
notificationIntent.putExtra(RemindersDbAdapter.KEY_eventid, rowId);
PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
Notification note=new Notification(android.R.drawable.stat_sys_warning, getString(R.string.notify_new_task_message), System.currentTimeMillis());
note.setLatestEventInfo(this, getString(R.string.notify_new_task_title), getString(R.string.notify_new_task_message), pi);
note.defaults |= Notification.DEFAULT_SOUND;
note.flags |= Notification.FLAG_AUTO_CANCEL;
// An issue could occur if user ever enters over 2,147,483,647 tasks. (Max int value).
// I highly doubt this will ever happen. But is good to note.
int id = (int)((long)rowId);
mgr.notify(id, note);
}
}
我这里有这个代码,它从assets文件夹复制数据库,然后将数据设置为一个局部变量,用于添加通知。我想自动设置数据来自数据库的提醒,但通知会一直读取点击按钮的日期和时间,而不是数据库中的日期和时间,因此程序的结果是触发通知和单击按钮后立即显示通知..请帮我这个。
我想根据数据库中的日期和时间设置通知。请帮我说明如何做到这一点。
答案 0 :(得分:0)
您是否尝试过设置断点
String datetime = c.getString(8);
并查看datetime变量中保留的时间?然后你可以找出时间错误的地方。你得到的是你日期的毫秒时间,但我认为你需要从中减去当前时间以获得你的提醒时间(time = when- System.currentTimeInMillis)。