vendredi 9 octobre 2015

How to generate notification by comparing date and time stored in SQLite DB with current date and time

I want raise a notification by comparing current date and time with the date's and time's stroed in the SQLite db.yyyy-mm-dd hh-mm both are in seperate column.

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

Aucun commentaire:

Enregistrer un commentaire