How to get time values ​​from a database and set an alarm to generate a notification

I have an application that will receive the time and date values ​​from the database, and it should be sent to the alarm clock to generate a notification ... but I am stuck how to get the date and time values ​​from the database and give the alarm manager to receive the notification .. I have many times and dates in the database ... I have to get the time and date and pass it to the alarm manager to generate a notification ... so far I have tried the code that it only generated for one day ... when I give a new alarm, the old ones are deleted ... only the last set alarm time is given to the alarm and its notification is only for the last set ... please help me guys ... `

Calendar ca = Calendar.getInstance();
ca.setTimeInMillis(System.currentTimeMillis());
int hr = ca.get(Calendar.HOUR)*60*60*1000;
int hr1 = ca.get(Calendar.MINUTE)*60*1000;
int hr2 = ca.get(Calendar.SECOND)*1000;

int cal = hr + hr1;

for(int y=1;y<h.length;y++){
    Toast.makeText(getApplicationContext(), "" +h[y],3000).show();
    String q[]=h[y].split(":");

    Integer i=Integer.parseInt(q[0]) * 60 * 60 * 1000;
    Integer i2=Integer.parseInt(q[1]) * 60 * 1000;

    long set= i + i2;
    long interval= set - cal;
    Toast.makeText(getApplicationContext(), "" +interval,3000).show();
    intr.add(interval);

    y++;
}

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();

for(int i = 0; i < intr.size(); ++i){
    Intent intentAlarm = new Intent(getApplicationContext(),AlarmReciever.class);
    PendingIntent p=PendingIntent.getBroadcast(getApplicationContext(), i, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager.set(AlarmManager.RTC_WAKEUP, ca.getTimeInMillis()+intr.get(i), p);

    //...
}

... , .

+3
1

Alarm

class, , , - , , . .

   AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            Intent intent = new Intent(getBaseContext(),
                    AlarmReceiver1.class);

            intent.putExtra("id", i);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(
                    context, i, intent, PendingIntent.FLAG_ONE_SHOT);
            alarmManager.set(AlarmManager.RTC_WAKEUP,
                    calSet.getTimeInMillis(), pendingIntent);

//

 id = intent.getIntExtra("id", 0);

//

 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                context)
                .setSmallIcon(com.card2contacts.R.drawable.appicon)
                .setContentTitle("Follow up with ")
                .setContentText("")
                .setSound(
                        RingtoneManager
                                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setDefaults(Notification.DEFAULT_VIBRATE)
                .setDefaults(Notification.FLAG_AUTO_CANCEL);
        Intent resultIntent = new Intent(context, FollowUp.class);
        // The stack builder object will contain an artificial back stack
        // for the started Activity.
        // This ensures that navigating backward from the Activity leads out
        // of your application to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(FollowUp.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
                0, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

        // Id allows you to update the notification later on.

        mNotificationManager.notify(id, mBuilder.build());
+1

All Articles