4

SOURCE CODE:

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

i have this code here that copies the database from the assets folder then sets the data to a local variable that will be used for adding a notification. i would like to automatically set the reminder which the data comes from the database but the notification keeps reading the date and time when the button is clicked and not the date and time from the database so the outcome of the program is it triggers the notification and shows the notification immediately after the button is clicked.. please help me with this.

i want set the notification based on the date and time from the database. please help me on how to do this.

sam
  • 133
  • 1
  • 2
  • 6

1 Answers1

0

Have you tried setting a break point here

 String datetime = c.getString(8);

and seeing what time is kept in the datetime variable? Then you can pinpoint where the time is going wrong. You are getting the time in milliseconds of your date but I think you need to subtract the current time from that to get your reminder time (time = when- System.currentTimeInMillis).

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • the data stored in datetime is in this kind of format -> "MM-dd-yyyy kk:mm:ss". what do you mean by adding breakpoint after the String datetime = c.getString(8);?? closing the database? – sam Sep 27 '12 at 23:47
  • the project is working okay if the setting of reminder is done thru date picker and time picker but when i changed the codes to automatically set it based from the data in the database, the notification did not show on the date and time set from the database instead it appears immediately after the button is clicked. – sam Sep 28 '12 at 00:03
  • For debugging. I don't know what editor you are using, I use eclipse, but set a breakpoint right there while debugging and you can see what the value of the variable is there. If it is the date that it is suppose to be, then you know you are retrieving it from the DB correctly and the problem is farther in the code. If it is not the date that is stored in the DB, then you are storing and/or retrieving it incorrectly. Let me know if that doesn't make sense or if you have any further issues – codeMagic Sep 28 '12 at 00:06
  • i also do use eclipse. when i toast the date `date = dateTimeFormat.parse(datetime);` it shows Fri Sept 14 08:00:00 GMT+08:00 2012 and the counterpart of it in the database is 09-14-2012 08:00:00 does that mean the formatting i used is wrong or what? – sam Sep 28 '12 at 00:15
  • No, the formatting is correct but you shouldn't be able to set a date for a day in the past. You need a conditional statement like if date < System.currentTimeInMillis { – codeMagic Sep 28 '12 at 00:38
  • // do something } else { //set reminder } You are getting the correct time from the database. Did you try subtracting the System.currentTimeInMillis() from the date in the reminderManager and send that to the AlarmManager? I think that may be where your problem is – codeMagic Sep 28 '12 at 00:44
  • I changed the dates in the database to a later date and time. the notification keeps on appearing after I click the button. i'll work on that. – sam Sep 28 '12 at 01:00