Reputation: 113
I have app that will get the time and date values from database and it should be given to alarm to generate notification...but I am stuck how to get values of date and time from database and give to alarm manager for getting notifications...I am having many times and dates in database...I have to fetch time and date and give it to alarm manager to generate notification...so far I have tried the code its only generating for a single day only...when I am giving new alarm the old ones are getting deleted...only last set alarm time is given to alarm manger and its giving notification only to the last one set...please help me out 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);
//...
}
Till now i am calculating the difference time between current time and stored time and giving to alarm manger for getting notifications...but i need all the values that stored in database according to the time and date..
Upvotes: 3
Views: 2095
Reputation: 616
You need to set Alarm with different ID's in your
Alarm Receiver
class, Here what happening for you is same ID replacing with older one so the latest alarm only will invoke. Please change the ID's based on the new alarm in your alarm receiver class.
I implement like this
in alarm setting
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);
In the receiver Class
//get the ID first
id = intent.getIntExtra("id", 0);
//set alarm
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());
Upvotes: 1