android wear-Showing Information in Watch Faces and Creating InteractiveWatch Faces, Providing Confi

desaco發表於2016-01-16

>Showing Information in Watch Faces

 In addition to telling time, Android Wear devices provide users with contextually relevant information in the form of cards, notifications, and other wearable apps. Creating a custom watch face not only gives you the opportunity to tell time in visually compelling ways, but also to show users relevant information whenever they glance at their device. 

 The CalendarWatchFaceService class obtains the number of meetings in the next day as follows:

/* Asynchronous task to load the meetings from the content provider and
 * report the number of meetings back using onMeetingsLoaded() */
private class LoadMeetingsTask extends AsyncTask<Void, Void, Integer> {
    @Override
    protected Integer doInBackground(Void... voids) {
        long begin = System.currentTimeMillis();
        Uri.Builder builder =
                WearableCalendarContract.Instances.CONTENT_URI.buildUpon();
        ContentUris.appendId(builder, begin);
        ContentUris.appendId(builder, begin + DateUtils.DAY_IN_MILLIS);
        final Cursor cursor = getContentResolver() .query(builder.build(),
                null, null, null, null);
        int numMeetings = cursor.getCount();
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG, "Num meetings: " + numMeetings);
        }
        return numMeetings;
    }

    @Override
    protected void onPostExecute(Integer result) {
        /* get the number of meetings and set the next timer tick */
        onMeetingsLoaded(result);
    }
}

This method initializes the timer when the watch face becomes visible:

@Override
public void onVisibilityChanged(boolean visible) {
    super.onVisibilityChanged(visible);
    if (visible) {
        mLoadMeetingsHandler.sendEmptyMessage(MSG_LOAD_MEETINGS);
    } else {
        mLoadMeetingsHandler.removeMessages(MSG_LOAD_MEETINGS);
        cancelLoadMeetingTask();
    }
}

> Creating Interactive Watch Faces

 Android Wear allows Android Wear watch faces to accept the single-tap gesture at a given location on the watch face, as long as there's not another UI element that also responds to that gesture.

 To provide a consistent user experience, the system reserves gestures such as drag and long-press for system UI elements. Therefore, the system does not send raw touch events to the watch face. Instead, the system forwards specific commands to the onTapCommand() method.

The following example shows you how to implement tap events on a watch face:

@Override
public void onTapCommand(
       @TapType int tapType, int x, int y, long eventTime) {
    switch (tapType) {
        case WatchFaceService.TAP_TYPE_TAP:
            hideTapHighlight();
            if (withinTapRegion(x, y)) {
                // Implement the tap action
                // (e.g. show detailed step count)
                onWatchFaceTap();
            }
            break;

        case WatchFaceService.TAP_TYPE_TOUCH:
            if (withinTapRegion(x, y)) {
                // Provide visual feedback of touch event
                startTapHighlight(x, y, eventTime);
            }
            break;

        case WatchFaceService.TAP_TYPE_TOUCH_CANCEL:
            hideTapHighlight();
            break;

        default:
            super.onTapCommand(tapType, x, y, eventTime);
            break;
    }
}

> Providing Configurion activities

 Users can choose the active watch face for their wearable device by selecting it on the companion app or using the watch face picker on the wearable device.Some watch faces support configuration parameters to let users customize how the watch face looks and behaves

To receive updated configuration parameters from the configuration activities, create a service that implements the WearableListenerService interface from the Wearable Data Layer API in your wearable app. Your watch face implementation can redraw the watch face when the configuration parameters change.

相關文章