Reputation: 477
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?
Upvotes: 4
Views: 5603
Reputation:
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 (source):
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.
Upvotes: 6