如何在 wearOS 中默认添加并发症 android

How to add complications by default in wearOS android

Developer.android 说:

void setDefaultSystemComplicationProvider (int watchFaceComplicationId, int 系统提供者, 整型)

但对我不起作用。

作为一名初学者 Android 开发人员,由于缺乏适用于 Wear OS 平台的社区支持和教程,我很难在默认情况下为 Wear OS 设置复杂功能。通过对我的代码进行一些调整,我以某种方式设法使该方法对我有用。我想分享给可能对他们有帮助的人。 这是我用过的方法:

private void initializeComplications() {
    Log.d("complication", "initializeComplications()");

    mActiveComplicationDataSparseArray = new SparseArray<>(COMPLICATION_IDS.length);

    ComplicationDrawable leftComplicationDrawable =
            (ComplicationDrawable) getDrawable(R.drawable.custom_complication_styles);
    leftComplicationDrawable.setContext(getApplicationContext());

    ComplicationDrawable rightComplicationDrawable =
            (ComplicationDrawable) getDrawable(R.drawable.custom_complication_styles);
    rightComplicationDrawable.setContext(getApplicationContext());

    ComplicationDrawable topComplicationDrawable =
            (ComplicationDrawable) getDrawable(R.drawable.custom_complication_styles);
    topComplicationDrawable.setContext(getApplicationContext());

    mComplicationDrawableSparseArray = new SparseArray<>(COMPLICATION_IDS.length);
    mComplicationDrawableSparseArray.put(LEFT_COMPLICATION_ID, leftComplicationDrawable);
    mComplicationDrawableSparseArray.put(RIGHT_COMPLICATION_ID, rightComplicationDrawable);
    mComplicationDrawableSparseArray.put(TOP_COMPLICATION_ID, topComplicationDrawable);

    // Set default complication
    // This time we don't create a setting screen so we need to set it here
    setDefaultSystemComplicationProvider (
            LEFT_COMPLICATION_ID,
            SystemProviders.STEP_COUNT,
            ComplicationData.TYPE_SHORT_TEXT);

    setDefaultSystemComplicationProvider (
            RIGHT_COMPLICATION_ID,
            SystemProviders.WATCH_BATTERY,
            ComplicationData.TYPE_SHORT_TEXT);

    setDefaultSystemComplicationProvider (
            TOP_COMPLICATION_ID,
            SystemProviders.DATE,
            ComplicationData.TYPE_SHORT_TEXT);

    setActiveComplications(COMPLICATION_IDS);
}

最后我调用了它:

public void onCreate(SurfaceHolder holder) {

    setWatchFaceStyle(new WatchFaceStyle.Builder(DigitalPrayerTimes.this)
            .setAcceptsTapEvents(true)
            .build());

    // TODO: Step 2, intro 3
    initializeComplications();
}