Reputation: 77
i'm adding alarm to my app. when the time for appointment set from user using timepicker, i want to alert user with alarm. if i set to alarm, it only takes the last alarm i set, how i want to make both working?..
there's no error, and i register already in Manefest. actually i want to fetch date and time set in sqlite and the alarm is based on the date and time.i'm getting stuck, first i want user to add appointment, let say 25/4/2013 on 4.30pm.. the data is save in the sqlite. then on the day, i want the apps, to alarm the user.. how can i do that? i know using alarm manager but i don't know where to start from the code
and for the alarmReceiver i want the class to be able to alert the user maybe using alert or toast or notification. but i have try alert and toast cannot be used. is there any other way? Please help me below is my code:
public class AppointmentAdd extends Activity
{
private SQLiteDatabase database;
private DBHelper helper;
private ScheduleClient scheduleClient;
Button btnSave, btnDate, btnTime;
EditText addPurpose;
DateFormat fmtDateAndTime = DateFormat.getDateTimeInstance();
Calendar myCalendar = Calendar.getInstance();
DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabelDate(year, monthOfYear, dayOfMonth);
}
};
TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
myCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
myCalendar.set(Calendar.MINUTE, minute);
updateLabelTime(hourOfDay, minute);
}
};
private void updateLabelDate(int year, int monthOfYear,
int dayOfMonth) {
year = myCalendar.get(Calendar.YEAR);
monthOfYear = myCalendar.get(Calendar.MONTH);
dayOfMonth = myCalendar.get(Calendar.DATE);
btnDate.setText(new StringBuilder().append(dayOfMonth).append(".")
.append(monthOfYear + 1).append(".").append(year).append(" "));
}
private void updateLabelTime(int hourOfDay, int minute) {
hourOfDay = myCalendar.get(Calendar.HOUR_OF_DAY);
minute = myCalendar.get(Calendar.MINUTE);
btnTime.setText(new StringBuilder().append(hourOfDay).append(":")
.append(minute));
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.appointment_add);
btnSave = (Button)findViewById(R.id.button3);
btnDate = (Button)findViewById(R.id.button1);
btnTime = (Button)findViewById(R.id.button2);
addPurpose = (EditText)findViewById(R.id.editText1);
scheduleClient = new ScheduleClient(this);
scheduleClient.doBindService();
btnDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new DatePickerDialog(AppointmentAdd.this, d, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
btnTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new TimePickerDialog(AppointmentAdd.this, t, myCalendar
.get(Calendar.HOUR_OF_DAY), myCalendar
.get(Calendar.MINUTE), true).show();
}
});
helper = new DBHelper(this);
database = helper.getWritableDatabase();
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String purpose = addPurpose.getText().toString();
String date = btnDate.getText().toString();
String time = btnTime.getText().toString();
helper.insertDataAppointment(database, date, time, purpose);
Toast.makeText(AppointmentAdd.this, "Successfully Add", Toast.LENGTH_SHORT).show();
setAlarm(myCalendar.get(Calendar.HOUR_OF_DAY), myCalendar.get(Calendar.MINUTE));
Intent i = new Intent(AppointmentAdd.this, AppointmentView.class);
startActivity(i);
}
});
updateLabelDate(0, 0, 0);
updateLabelTime(0, 0);
}
private void setAlarm(int Hour, int Minute){
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, Hour);
cal.set(Calendar.MINUTE, Minute);
cal.set(Calendar.SECOND, 0);
//in case of choosing a previous hour, then set alarm to next day
if (cal.getTimeInMillis() < System.currentTimeMillis())
cal.set(Calendar.HOUR_OF_DAY, Hour + 24);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent Notifyintent = new Intent(this, Notify.class);
PendingIntent Notifysender = PendingIntent.getBroadcast(this, 0, Notifyintent, PendingIntent.FLAG_UPDATE_CURRENT);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 3600000, Notifysender);
Toast.makeText(AppointmentAdd.this, "Alarm set successfully" , Toast.LENGTH_LONG).show();
}
public void onClickCancel(View v){
startActivity (new Intent(getApplicationContext(), AppointmentView.class));
}
} // end class
and here my AlarmReceiver class:
public class AlarmReceiver extends BroadcastReceiver {
@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager myNotificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "Update Device", 0);
Intent notificationIntent = new Intent(context, AppointmentView.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, "Appointment", "Please check your appointment list", contentIntent);
notification.flags |= Notification.FLAG_HIGH_PRIORITY;
myNotificationManager.notify(0, notification);
}
}
Upvotes: 0
Views: 1294
Reputation: 1
You can try this:
final int alarmId = (int) System.currentTimeMillis();
PendingIntent pi =
PendingIntent.getBroadcast(Date_Time.this, alarmId, i,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager manager = (AlarmManager) getSystemService(Date_Time.this.ALARM_SERVICE);
manager.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis() , pi);
Upvotes: 0
Reputation: 6751
As per your comment:
It's because the pendingintent
needs different request id each time.
Now your present PendingIntent is in the below way:
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Here you are giving reqid
as 0 every time, so instead of different pending intent being created, it's overriding the present pending intent, that is why you get the latest set alarm fired. So give unique request id with different number everytime like below.
PendingIntent pi = PendingIntent.getBroadcast(this, reqid, intent,PendingIntent.FLAG_UPDATE_CURRENT);
Even I had done the same thing as per your requirement which I had to store the entire data in the sqlite database about the messages and alarms. So, to set different request id
each time, I have used primary key id which is in database to set for the pending intent. Hope this helps.
Upvotes: 0