Android磨损模块监听器服务不接收来自移动模块的数据

时间:2015-03-06 04:32:27

标签: android wear-os android-wear-data-api watch-face-api

我正在开发一个样品表面,用户可以在移动应用程序中选择一些颜色配置,触发磨损应用程序表面上的更改。

以下是我在移动应用中发送配置的内容:

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>

这是我在穿着中实现的听众服务:

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方法。有什么我想念的吗?

2 个答案:

答案 0 :(得分:0)

在数据项地图中添加键值

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

再次发送

答案 1 :(得分: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清单中,只需将这些行添加到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
两个清单中的

必须相同。