Make Android Custom Alarm Clock for Reminder

I am making a medicine reminder app. In this application, I have included a doctor’s appointment reminder. In my application, the user is allowed to set their own date and time, and an alarm goes off for that date and time. Please help me .. and I take the link from this topic .. How to set an alarm on Android?

+4
source share
1 answer

Use the following steps.

For calendar reminder:

1.Create an 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 a reminder and set it using a code like:

 // 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 Resolution:

 <receiver android:process=":remote" android:name="AlarmReceiver"></receiver> 

3.Set 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 the 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(); } } 

Change the code if necessary.

+6
source

All Articles