如何将数据暴露给手动触发的表盘

问题描述

我是 Android Wear OS 的新手。我想将数字数据 (0,1,2,3,...,9) 公开到 Android Smartwatch 的数据字段(手动触发而不点击数据字段)

如此处所述,

https://developer.android.com/codelabs/data-providers#0

我已经实现了 onReceive() / onComplicationUpdate() 方法

public class ComplicationTapbroadcastReceiver extends broadcastReceiver {

private static final String EXTRA_PROVIDER_COMPONENT =
        "com.example.android.wearable.watchface.provider.action.PROVIDER_COMPONENT";
private static final String EXTRA_COMPLICATION_ID =
        "com.example.android.wearable.watchface.provider.action.COMPLICATION_ID";

static final int MAX_NUMBER = 20;
static final String COMPLICATION_PROVIDER_PREFERENCES_FILE_KEY =
        "com.example.android.wearable.watchface.COMPLICATION_PROVIDER_PREFERENCES_FILE_KEY";

@Override
public void onReceive(Context context,Intent intent) {
    Bundle extras = intent.getExtras();
    ComponentName provider = extras.getParcelable(EXTRA_PROVIDER_COMPONENT);
    int complicationId = extras.getInt(EXTRA_COMPLICATION_ID);

    // Retrieve data via SharedPreferences.
    String preferenceKey = getPreferenceKey(provider,complicationId);
    SharedPreferences sharedPreferences =
            context.getSharedPreferences(COMPLICATION_PROVIDER_PREFERENCES_FILE_KEY,0);

    int value = sharedPreferences.getInt(preferenceKey,0);

    // Update data for complication.
    value = (value + 1) % MAX_NUMBER;

    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt(preferenceKey,value);
    editor.apply();

    // Request an update for the complication that has just been tapped.
    ProviderUpdateRequester requester = new ProviderUpdateRequester(context,provider);
    requester.requestUpdate(complicationId);
}

/**
 * Returns a pending intent,suitable for use as a tap intent,that causes a complication to be
 * toggled and updated.
 */
static PendingIntent getToggleIntent(
        Context context,ComponentName provider,int complicationId) {
    Intent intent = new Intent(context,ComplicationTapbroadcastReceiver.class);
    intent.putExtra(EXTRA_PROVIDER_COMPONENT,provider);
    intent.putExtra(EXTRA_COMPLICATION_ID,complicationId);

    // Pass complicationId as the requestCode to ensure that different complications get
    // different intents.
    return PendingIntent.getbroadcast(
            context,complicationId,intent,PendingIntent.FLAG_UPDATE_CURRENT);
}

/**
 * Returns the key for the shared preference used to hold the current state of a given
 * complication.
 */
static String getPreferenceKey(ComponentName provider,int complicationId) {
    return provider.getClassName() + complicationId;
}
}

public class CustomComplicationProviderService extends ComplicationProviderService {

private static final String TAG = "ComplicationProvider";

/*
 * Called when a complication has been activated. The method is for any one-time
 * (per complication) set-up.
 *
 * You can continue sending data for the active complicationId until onComplicationDeactivated()
 * is called.
 */
@Override
public void onComplicationActivated(
        int complicationId,int dataType,ComplicationManager complicationManager) {
    Log.d(TAG,"onComplicationActivated(): " + complicationId);
}

/*
 * Called when the complication needs updated data from your provider. There are four scenarios
 * when this will happen:
 *
 *   1. An active watch face complication is changed to use this provider
 *   2. A complication using this provider becomes active
 *   3. The period of time you specified in the manifest has elapsed (UPDATE_PERIOD_SECONDS)
 *   4. You triggered an update from your own class via the
 *       ProviderUpdateRequester.requestUpdate() method.
 */
@Override
public void onComplicationUpdate(
        int complicationId,"onComplicationUpdate() id: " + complicationId);

    // Create Tap Action so that the user can trigger an update by tapping the complication.
    ComponentName thisProvider = new ComponentName(this,getClass());
    // We pass the complication id,so we can only update the specific complication tapped.
    PendingIntent complicationPendingIntent =
            ComplicationTapbroadcastReceiver.getToggleIntent(
                    this,thisProvider,complicationId);

    // Retrieves your data,in this case,we grab an incrementing number from SharedPrefs.
    SharedPreferences preferences =
            getSharedPreferences(
                    ComplicationTapbroadcastReceiver.COMPLICATION_PROVIDER_PREFERENCES_FILE_KEY,0);
    int number =
            preferences.getInt(
                    ComplicationTapbroadcastReceiver.getPreferenceKey(
                            thisProvider,complicationId),0);
    String numberText = String.format(Locale.getDefault(),"%d!",number);

    ComplicationData complicationData = null;

    switch (dataType) {
        case ComplicationData.TYPE_SHORT_TEXT:
            complicationData =
                    new ComplicationData.Builder(ComplicationData.TYPE_SHORT_TEXT)
                            .setShortText(ComplicationText.plainText(numberText))
                            .setTapAction(complicationPendingIntent)
                            .build();
            break;
        case ComplicationData.TYPE_LONG_TEXT:
            complicationData =
                    new ComplicationData.Builder(ComplicationData.TYPE_LONG_TEXT)
                            .setLongText(ComplicationText.plainText("Number: " + numberText))
                            .setTapAction(complicationPendingIntent)
                            .build();
            break;
        case ComplicationData.TYPE_RANGED_VALUE:
            complicationData =
                    new ComplicationData.Builder(ComplicationData.TYPE_RANGED_VALUE)
                            .setValue(number)
                            .setMinValue(0)
                            .setMaxValue(ComplicationTapbroadcastReceiver.MAX_NUMBER)
                            .setShortText(ComplicationText.plainText(numberText))
                            .setTapAction(complicationPendingIntent)
                            .build();
            break;
        default:
            if (Log.isLoggable(TAG,Log.WARN)) {
                Log.w(TAG,"Unexpected complication type " + dataType);
            }
    }

    if (complicationData != null) {
        complicationManager.updateComplicationData(complicationId,complicationData);

    } else {
        // If no data is sent,we still need to inform the ComplicationManager,so the update
        // job can finish and the wake lock isn't held any longer than necessary.
        complicationManager.noUpdaterequired(complicationId);
    }
}

/*
 * Called when the complication has been deactivated.
 */
@Override
public void onComplicationDeactivated(int complicationId) {
    Log.d(TAG,"onComplicationDeactivated(): " + complicationId);
}
}

代码正在运行,但是...

a) 如何使用我的传感器信息更新智能手表数据字段的显示无需点击智能手表的数据字段?甚至有可能吗?

b) 我必须如何配置 AndroidManifest.xml,以及如何在我的 Java 应用程序中实现 ProviderUpdateRequester 方法 requestUpdateall() 以手动触发显示更新?

new ProviderUpdateRequester(context,new ComponentName(context,"myComplicationClass"))
        .requestUpdateall();

在我的案例中什么是“上下文”,什么是“myComplicationClass”?

不幸的是,我还没有找到任何好的代码示例...

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)