i am making medicine reminder app,In that app i have included facility of adding a reminder about doctor's appointment. In my application user is allowed to set his/her own date and time and on that date and time alarm should triggered.. please help me.. and i have take reference from this thread.. How to set Alarm in Android?
Asked
Active
Viewed 5,556 times
4
-
i have tried alarm manager,pending intent and everything but yet not get the desired result.. – Riddhi Shah Mar 26 '13 at 11:44
-
1http://android-er.blogspot.in/2010/10/simple-example-of-alarm-service-using.html – Riddhi Shah Mar 26 '13 at 11:45
-
http://code4reference.com/2012/07/tutorial-on-android-alarmmanager/ – Riddhi Shah Mar 26 '13 at 11:45
-
https://web.archive.org/web/20130724094711/http://justcallmebrian.com/2010/04/27/using-alarmmanager-to-schedule-activities-on-android/ – General Grievance Aug 28 '23 at 15:40
1 Answers
6
Use the following steps.
For reminder in calendar:
1.Create a event as:
// get calendar
Calendar cal = Calendar.getInstance();
Uri EVENTS_URI = Uri.parse(getCalendarUriBase(this) + "events");
ContentResolver cr = getContentResolver();
// event insert
ContentValues values = new ContentValues();
values.put("calendar_id", 1);
values.put("title", "Reminder Title");
values.put("allDay", 0);
values.put("dtstart", cal.getTimeInMillis() + 11*60*1000); // event starts at 11 minutes from now
values.put("dtend", cal.getTimeInMillis()+60*60*1000); // ends 60 minutes from now
values.put("description", "Reminder description");
values.put("visibility", 0);
values.put("hasAlarm", 1);
Uri event = cr.insert(EVENTS_URI, values);
2.Then create reminder and set it useing code as:
// reminder insert
Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(this) + "reminders");
values = new ContentValues();
values.put( "event_id", Long.parseLong(event.getLastPathSegment()));
values.put( "method", 1 );
values.put( "minutes", 10 );
cr.insert( REMINDERS_URI, values );
3.Set permissions as:
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
For Alarm only:
1.Create a broadcast receiver as:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
Bundle bundle = intent.getExtras();
String message = bundle.getString("alarm_message");
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
2.Set permission:
<receiver android:process=":remote" android:name="AlarmReceiver"></receiver>
3.Set up event:
// get a Calendar object with current time
Calendar cal = Calendar.getInstance();
// add 5 minutes to the calendar object
cal.add(Calendar.MINUTE, 5);
Intent intent = new Intent(ctx, AlarmReceiver.class);
intent.putExtra("alarm_message", "O'Doyle Rules!");
// In reality, you would want to have a static variable for the request code instead of 192837
PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
4.Activity from broadcast receiver:
@Override
public void onReceive(Context context, Intent intent) {
try {
Bundle bundle = intent.getExtras();
String message = bundle.getString("alarm_message");
Intent newIntent = new Intent(context, AlarmActivity.class);
newIntent.putExtra("alarm_message", message);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(newIntent);
} catch (Exception e) {
Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
Modify the code as needed.

Pang
- 9,564
- 146
- 81
- 122

Manoj Fegde
- 4,786
- 15
- 50
- 95
-
i dont need to put this things to calender.. i just want when user saves his/her doctor details at that time alarm should set and on desired time it should be triggered.. – Riddhi Shah Mar 26 '13 at 11:53
-
i need reminder on disered time that on so on so date you have appointment of Dr.X.i hope you got my question. do you? – Riddhi Shah Mar 26 '13 at 12:00
-
So you can get time and date in edit text from user or from picker view and set it to event by variables. – Manoj Fegde Mar 26 '13 at 12:03
-
-
yes it is. You can use picker for easy way of getting input from user. and then get that input in variable and set that variable to event by which you can set reminder or alarms. – Manoj Fegde Mar 26 '13 at 12:05
-
i already did this. i took date's value from date picker and time's value from time picker. but came with no result.. – Riddhi Shah Mar 26 '13 at 12:07
-
i have taken reference from this thread http://stackoverflow.com/questions/6520403/how-to-set-alarm-in-android – Riddhi Shah Mar 28 '13 at 08:30
-
Thanks for the answer - I have a further question: If you are passing extras with your receiver intent to the activity do you need to use setComponent()? The sample code suggests you need to use setComponent() to indicate that a SERVICE should handle the receiver's intent if you are including extras. Is this only with a SERVICE not with an ACTIVITY from the receiver? – samleighton87 Mar 01 '15 at 11:52