(Difference between revisions)

Using the PIM API to create Medication Reminders

From Nokia Developer Wiki
Jump to: navigation, search
igordsm (Talk | contribs)
(Igordsm -)
igordsm (Talk | contribs)
(Igordsm -)
Line 129: Line 129:
For each Medication Reminder, we need to create (number of days)*(times per day) calendar events, each starting at the correct time. We put the code to create these events in the ''commandAction'' method.
For each Medication Reminder, we need to create (number of days)*(times per day) calendar events, each starting at the correct time. We put the code to create these events in the ''commandAction'' method.
[[File:Ss3-med-reminder.png|thumb|The reminders appear in the user's calendar.]]
<code java>
<code java>

Revision as of 04:22, 19 November 2013

This article explains how to use Calendar Events using the PIM API (JSR-75) to create Medication Reminders.

Article Metadata
Created: igordsm (12 Nov 2013)
Last edited: igordsm (19 Nov 2013)

Note.pngNote: This is an entry in the Nokia Asha Wiki Competition 2013H2.



A medication reminder in the Faslane.

Mobile and smart phones can be used to improve their owners health. A common problem nowadays is that people often forget to take their medications at the correct time. This reduces the medicine's effect and, sometimes, puts in risk the whole treatment. In this article we describe how to create a Medication Reminder for the new Asha mobile phones.

The solution described in this article uses Calendar events to remind the patients to take their medications. Calendar events are suited for this task because

  1. they do not depend on an internet connection (as the Notifications API);
  2. push registry notifications disappear completely after they are dismissed (or if the time has passed and the phone is turned off);
  3. they remain on the device and appear on the calendar;
  4. calendar events appear in the fastlane.

The code

Create a new Midlet project in Nokia IDE and open Main.java.

User interface

In this example we use a simple LCDUI. Our interface will contain fields for the medication name, the frequency it should be taken and the dates to start and stop taking the medicine.

Screenshot of the interface.

Add the following variables to our Midlet.

Form f;
TextField name;
DateField start;
DateField end;
TextField every;

We build the form in the startApp method and show it to the user.

protected void startApp() throws MIDletStateChangeException {
f = new Form("Medication Reminder");
name = new TextField("Medication name", "", 20, TextField.ANY);
start = new DateField("Start time", DateField.DATE_TIME);
days = new TextField("How many days", "1", 2, TextField.NUMERIC);
frequency = new TextField("Times per day", "2", 2, TextField.NUMERIC);
f.addCommand(new Command("Add", Command.OK, 1));

Also, add the function below. It will be useful later.

protected void resetFields() {
start.setDate(new Date(System.currentTimeMillis()));

Adding events using the PIM API

The Nokia Asha platform implements the PIM API (JSR-75), which gives us access to the user's Calendar. We are interested in the EventList and Event classes.

The EventList holds all the events in the phone's calendar and is accessed using the .openPIMList method. The Event class represents an appointment in the calendar. Every Event must be created by an opened EventList using the EventList.createEvent methods and the fields must be filled using the Event.add* methods. Since the Nokia Asha Platform supports all the fields in the JSR, there is no need to check if they exist. To save the modifications to an Event, call the commit method. Below is an example on how to add an Event to the Calendar

try {
EventList evts = (EventList) PIM.getInstance().openPIMList(PIM.EVENT_LIST, PIM.WRITE_ONLY);
Event e = evts.createEvent();
e.addString(Event.SUMMARY, PIMItem.ATTR_NONE, "Event Name");
e.addDate(Event.START, PIMItem.ATTR_NONE, date.getTime());
e.addDate(Event.END, PIMItem.ATTR_NONE, date.getTime());
e.addInt(Event.ALARM, PIMItem.ATTR_NONE, 1); //
} catch (PIMException e) {

The following method will be used to add our calendar events. Since we will add many entries for each Medication Reminder, we take an opened EventList as argument.

public void addEvent(EventList evts, String summary, long start, long end) {
Event e = evts.createEvent();
/* set fields */
e.addString(Event.SUMMARY, PIMItem.ATTR_NONE, name.getString());
e.addDate(Event.START, PIMItem.ATTR_NONE, start);
e.addDate(Event.END, PIMItem.ATTR_NONE,end);
e.addInt(Event.ALARM, PIMItem.ATTR_NONE, 1);
try {
} catch (PIMException e1) {

For each Medication Reminder, we need to create (number of days)*(times per day) calendar events, each starting at the correct time. We put the code to create these events in the commandAction method.

The reminders appear in the user's calendar.
public void commandAction(Command c, Displayable d) {
try {
EventList evts = (EventList) PIM.getInstance().openPIMList(PIM.EVENT_LIST, PIM.WRITE_ONLY);
int freq = Integer.parseInt(frequency.getString());
long num_days = Integer.parseInt(days.getString());
long n = freq * num_days;
int time_interval = 3600 * 24 / freq * 1000;
for (int i = 0; i < n; i++) {
addEvent(evts, name.getString(),
start.getDate().getTime() + time_interval * i,
start.getDate().getTime() + time_interval * i);
} catch (PIMException e) {
Alert ok = new Alert("Reminders created.");
Display.getDisplay(this).setCurrent(ok, f);

The interval between the events is the number of times the medicine needs to be taken each day (24 / freq) times the number of miliseconds in each hour (3600*1000).


247 page views in the last 30 days.