Reputation: 1
Is it possible to set a reminder that will alert a user from both a watch and a connected phone, programmatically, in Android, using Wear OS (that is, via an Android smart watch)? I can use the reminder app on my watch to set a reminder, but I cannot do this from an app that I’m developing that is executing on the watch.
I am told that the way to programmatically set a reminder (on a phone) is to use two steps, and the first step is to Send Data from Wear OS to Phone. Specifically, on the Wear OS device, use the Data Layer API to send event details to the phone.
import com.google.android.gms.tasks.Task; import com.google.android.gms.wearable.DataClient; import com.google.android.gms.wearable.DataMap;
import com.google.android.gms.wearable.PutDataMapRequest;
import com.google.android.gms.wearable.PutDataRequest;
import com.google.android.gms.wearable.Wearable;
public void sendEventToPhone(String title, long startTime, long endTime, String location) {
PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/calendar_event");
putDataMapReq.getDataMap().putString("title", title);
putDataMapReq.getDataMap().putLong("startTime", startTime);
putDataMapReq.getDataMap().putLong("endTime", endTime);
putDataMapReq.getDataMap().putString("location", location);
PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
Task<DataItem> putDataTask = Wearable.getDataClient(this).putDataItem(putDataReq);
}
The Next step is to receive data on the phone and insert an event by implementing a WearableListenerService to receive the data and insert the event into the calendar.
import android.content.ContentValues;
import android.net.Uri;
import android.provider.CalendarContract;
import com.google.android.gms.wearable.DataEvent;
import com.google.android.gms.wearable.DataEventBuffer;
import com.google.android.gms.wearable.DataMap;
import com.google.android.gms.wearable.DataMapItem;
import com.google.android.gms.wearable.WearableListenerService;
public class CalendarEventService extends WearableListenerService {
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
for (DataEvent event : dataEvents) {
if (event.getType() == DataEvent.TYPE_CHANGED) {
DataMap dataMap =
DataMapItem.fromDataItem(event.getDataItem()).getDataMap();
if (event.getDataItem().getUri().getPath().equals("/calendar_event")) {
String title = dataMap.getString("title");
long startTime = dataMap.getLong("startTime");
long endTime = dataMap.getLong("endTime");
String location = dataMap.getString("location");
insertEvent(title, startTime, endTime, location);
}
}
}
}
private void insertEvent(String title, long startTime, long endTime, String location) {
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, startTime);
values.put(CalendarContract.Events.DTEND, endTime);
values.put(CalendarContract.Events.TITLE, title);
values.put(CalendarContract.Events.EVENT_LOCATION, location);
values.put(CalendarContract.Events.CALENDAR_ID, 1); // Use the default calendar
values.put(CalendarContract.Events.EVENT_TIMEZONE, "UTC");
Uri uri = getContentResolver().insert(CalendarContract.Events.CONTENT_URI, values);
if (uri != null) {
long eventId = Long.parseLong(uri.getLastPathSegment());
// Optionally, add a reminder
ContentValues reminderValues = new ContentValues();
reminderValues.put(CalendarContract.Reminders.EVENT_ID, eventId);
reminderValues.put(CalendarContract.Reminders.MINUTES, 15);
reminderValues.put(CalendarContract.Reminders.METHOD,
CalendarContract.Reminders.METHOD_ALERT);
getContentResolver().insert(CalendarContract.Reminders.CONTENT_URI,
reminderValues);
}
}
}
However, this will not work for my application, as I need the reminder to be set without the phone being powered on. The reason for this is that if the user needs to access the phone, there is no point in using the watch.
Here are some things I’ve tried that don’t work:
First, I tried the following code using WearableCalendarContract, and the code threw an exception indicating that inserts with WearableCalendarContract are not allowed.
public long createInstance(ContentResolver contentResolver, long i_eventId, long
beginTime, long endTime) {
ContentValues values = new ContentValues();
values.put(CalendarContract.Instances.EVENT_ID, i_eventId);
values.put(CalendarContract.Instances.BEGIN, beginTime);
values.put(CalendarContract.Instances.END, endTime);
Uri uri = null;
try {
uri = contentResolver.insert(WearableCalendarContract.Instances.CONTENT_URI,
values);
}
catch (Exception e){
int x = 1;
}
Cursor cursor = contentResolver.query(WearableCalendarContract.Instances.CONTENT_URI,
new String[]{CalendarContract.Reminders.EVENT_ID }, null, null, null);
int wNumInstances = cursor.getCount();
if (uri == null) {
return Long.parseLong(uri.getLastPathSegment());
}
// Prepare reminder values
values = new ContentValues();
// eventId is the ID of the event
values.put(CalendarContract.Reminders.EVENT_ID, i_eventId);
// or METHOD_EMAIL
values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
values.put(CalendarContract.Reminders.MINUTES, 1); // Reminder 30 minutes before event
// Insert reminder
android.net.Uri uri2 =
contentResolver.insert(WearableCalendarContract.Reminders.CONTENT_URI, values);
Cursor cursor2 = contentResolver.query(WearableCalendarContract.Reminders.CONTENT_URI,
new String[]{CalendarContract.Reminders.EVENT_ID }, null, null, null);
int wNumReminder = cursor2.getCount();
return Long.parseLong(uri2.getLastPathSegment());
}
Next: I tried modifying the code to use CalendarContract. This involved changing CalendarContract.Instances.EVENT_ID to CalendarContract.Events.EVENT_ID and related changes for BEGIN & END. The code no longer threw an exception when the insert was executed, yet the insert did not work. This was confirmed by examining the instance count in the wNumInstances = cursor.getCount() line of code indicating that wNumInstances= 0, meaning that no items have been inserted. Is there any way to resolve these issues and add a reminder?
Finally, I tried to find an API that can be used with the Reminder app on the phone (the Reminder app, somehow, can add reminders). My search did not find any API that would allow my app to set reminders through the Reminder app, nor did it show any other API.
Upvotes: 0
Views: 83
Reputation: 64
The first solution you are mentioning is also how I would address the problem.
Since your phone might be off or out of range when the watch is scheduling the events, you need to :
Upvotes: 0