如何通过将SQLite DB中存储的日期和时间与当前日期和时间进行比较来生成通知

时间:2015-10-09 07:07:40

标签: android sqlite

我想通过将当前日期和时间与SQLite db.yyyy-mm-dd hh-mm中存储的日期和时间进行比较来提出通知。两者都在单独的列中。

private void startAlarm() {

    String[] dateArray=selectedDate.split("-");
    int dat[]=new int[dateArray.length];
    for(int i=0;i<dateArray.length;i++)
    {
        String d=dateArray[i];
        dat[i]=Integer.parseInt(d);
    }
    AlarmManager alarmManager = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.set(dat[2],dat[1],dat[0],selectedHour,selectedMinute);




public class ReminderService extends IntentService {

 private static final int NOTIF_ID = 1;

    public ReminderService(){
        super("ReminderService");
    }

    @Override
      protected void onHandleIntent(Intent intent) {
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        long when = System.currentTimeMillis(); 
        System.out.println(when);
        Notification notification = new Notification(R.drawable.alarm, "reminder", when);
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.flags |= notification.FLAG_AUTO_CANCEL;
        Intent notificationIntent = new Intent(this,MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent , 0);
        notification.setLatestEventInfo(getApplicationContext(), "hiiiiiii","notification", contentIntent);
        nm.notify(NOTIF_ID, notification);
    }


}
   System.out.println(""+dat[2]+""+dat[1]+""+dat[0]+""+selectedHour+""+selectedMinute+"##########################");
    long when = calendar.getTimeInMillis();
    System.out.println(when+"$$$$$$$$$$$$$$$$$$$$$$$$$$");
    Intent intent = new Intent(this,ReminderService.class);
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
    alarmManager.set(AlarmManager.RTC_WAKEUP, when, pendingIntent);
}

1 个答案:

答案 0 :(得分:0)

您可以将它们作为时间戳进行比较,以毫秒为单位构建一个String,如下例所示:

import java.sql.Timestamp;
import java.util.Date;


String yourDatabaseSavedDate = "2015-10-09 09:00:00.000000000";
Date date = new Date();
Timestamp timestamp = new Timestamp(date.getTime());
if(timestamp.getTime() >= Timestamp.valueOf(yourDatabaseSavedDate).getTime())
{
   System.out.println("ALARM!");
}
相关问题