0

我正在开发一个示例表盘,其中用户可以在移动应用程序上选择一些颜色配置,从而触发穿戴应用程序表盘的更改。

这是我在移动应用程序中发送配置的内容:

package com.quicklyjava.templatewatchface;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.wearable.DataApi;
import com.google.android.gms.wearable.PutDataMapRequest;
import com.google.android.gms.wearable.PutDataRequest;
import com.google.android.gms.wearable.Wearable;

/**
 * Created by P
 */
public class WearConnection {

private final GoogleApiClient mGoogleApiClient;
private String TAG = "wear.bridge";

public WearConnection(Context context) {
    mGoogleApiClient = new GoogleApiClient.Builder(context)
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle connectionHint) {
                    Log.d(TAG, "onConnected: " + connectionHint);
                }

                @Override
                public void onConnectionSuspended(int cause) {
                    Log.d(TAG, "onConnectionSuspended: " + cause);
                }
            })
            .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult result) {
                    Log.d(TAG, "onConnectionFailed: " + result);
                }
            })
            .addApi(Wearable.API)
            .build();

}

public void connect(){
    mGoogleApiClient.connect();
}

public void disconnect() {
    mGoogleApiClient.disconnect();
}

public void sendColor(String path, int color) {
    PutDataMapRequest dataMap = PutDataMapRequest.create(path);
    dataMap.getDataMap().putInt("value", color);
    PutDataRequest request = dataMap.asPutDataRequest();
    PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi.putDataItem(mGoogleApiClient, request);
    pendingResult.setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
        @Override
        public void onResult(DataApi.DataItemResult dataItemResult) {
            Log.d("wear", "sent: " + dataItemResult);
        }
    });

}

}

这是移动端的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.quicklyjava.templatewatchface" >

<uses-permission android:name="com.google.android.permission.PROVIDE_BACKGROUND" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>


    </activity>
</application>

这是我在wear中实现的监听服务:

package com.quicklyjava.templatewatchface;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.data.FreezableUtils;
import com.google.android.gms.wearable.DataEvent;
import com.google.android.gms.wearable.DataEventBuffer;
import com.google.android.gms.wearable.DataMapItem;
import com.google.android.gms.wearable.Node;
import com.google.android.gms.wearable.Wearable;
import com.google.android.gms.wearable.WearableListenerService;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by P
 */
public class TemplateWatchConfigListenerService extends WearableListenerService {

private static final String TAG = "DataLayerSample";
private SharedPreferences preferences;

//  keys for the data map
private String KEY_DOT_COLOR="com.quicklyjava.backgroundcolor";

GoogleApiClient mGoogleApiClient;

@Override
public void onCreate() {
    super.onCreate();

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .build();

    mGoogleApiClient.connect();

    preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
}

@Override
public void onDataChanged(DataEventBuffer dataEvents) {
    LOGD(TAG, "onDataChanged: " + dataEvents);

    final List<DataEvent> events = FreezableUtils.freezeIterable(dataEvents);
    dataEvents.close();

    if(!mGoogleApiClient.isConnected()) {
        ConnectionResult connectionResult = mGoogleApiClient.blockingConnect(30, TimeUnit.SECONDS);

        if (!connectionResult.isSuccess()) {
            Log.e(TAG, "WatchFaceConfigListenerService failed to connect to GoogleApiClient.");
            return;
        }
    }

    // Loop through the events and send a message back to the node that created the data item.
    for (DataEvent event : events) {
        Uri uri = event.getDataItem().getUri();
        String path = uri.getPath();

        DataMapItem item = DataMapItem.fromDataItem(event.getDataItem());

        switch (path) {
            case "/backgroundColor":
                putIntPreference(KEY_DOT_COLOR, item.getDataMap().getInt("value"));
                break;

            default:
                Log.e("TempWatchList","Default");

        }

        getBaseContext().sendBroadcast(new Intent("com.quicklyjava.action.config_changed"));

    }
}

@Override
public void onPeerConnected(Node peer) {
    LOGD(TAG, "onPeerConnected: " + peer);
}

@Override
public void onPeerDisconnected(Node peer) {
    LOGD(TAG, "onPeerDisconnected: " + peer);
}

public static void LOGD(final String tag, String message) {
    if (Log.isLoggable(tag, Log.DEBUG)) {
        Log.d(tag, message);
    }
}

private void putIntPreference(String key, int value) {
    preferences
            .edit()
            .putInt(key, value)
            .apply();
}
}

最后是磨损清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.quicklyjava.templatewatchface" >

<uses-sdk android:minSdkVersion="21"
    android:targetSdkVersion="21" />

<uses-feature android:name="android.hardware.type.watch" />

<!-- Required to act as a custom watch face. -->
<uses-permission android:name="com.google.android.permission.PROVIDE_BACKGROUND" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >


    <service
        android:name=".TemplateWatchService"
        android:label="@string/template_name"
        android:permission="android.permission.BIND_WALLPAPER" >
        <meta-data
            android:name="android.service.wallpaper"
            android:resource="@xml/watch_face" />
        <meta-data
            android:name="com.google.android.wearable.watchface.preview"
            android:resource="@drawable/preview_digital" />
        <meta-data
            android:name="com.google.android.wearable.watchface.preview_circular"
            android:resource="@drawable/preview_digital_circular" />
        <intent-filter>
            <action android:name="android.service.wallpaper.WallpaperService" />
            <category android:name="com.google.android.wearable.watchface.category.WATCH_FACE" />
        </intent-filter>
    </service>

    <service android:name=".TemplateWatchConfigListenerService" >
        <intent-filter>
            <action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
        </intent-filter>
    </service>

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

</application>

调用移动应用中的 sendColor 方法时,不会触发 onDataChanged 方法。有什么我想念的吗?

4

2 回答 2

0

在数据项映射中添加键值

map.put("system", System.currentmillions())

并再次发送

于 2015-03-10T03:28:42.087 回答
0

是的,您缺少一些重要的元数据标签

在磨损清单中,将这些标签添加到 WatchFaceService 的服务标签中:

<meta-data <!-- Companion config -->
            android:name="com.google.android.wearable.watchface.companionConfigurationAction"
            android:value="com.example.package.CONFIG_FACE" />

<meta-data <!-- wear config -->
            android:name="com.google.android.wearable.watchface.wearableConfigurationAction"
            android:value="com.example.package.CONFIG_FACE" />

在您的 Companion manifest 中,只需将这些行添加到 CompanionConfigActivity 的活动标签中:

<intent-filter>
            <action android:name="com.example.package.CONFIG_FACE" />

            <category android:name="com.google.android.wearable.watchface.category.COMPANION_CONFIGURATION" />
            <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

编码 :

com.example.package.CONFIG_FACE

两个清单中必须相同。

于 2016-01-27T08:38:24.347 回答