简体   繁体   English

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

[英]Android wear module listener service not receiving data from mobile module

I am developing a sample watch face wherein a user would be able to select some color configuration on mobile app triggering changes on wear app watch face. 我正在开发一个样品表面,其中用户可以在移动应用程序上选择一些颜色配置,从而触发磨损app表面上的更改。

Here is what I have in mobile app to send configuration: 以下是我在移动应用程序中发送配置的内容:

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);
        }
    });

}

}

Here is the manifest for mobile: 这是移动的清单:

<?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>

Here is the listener service I implemented in 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();
}
}

Finally here is the wear manifest: 最后这里是磨损清单:

<?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>

The onDataChanged method is not getting triggered when the sendColor method in mobile app is called. 调用移动应用程序中的sendColor方法时,不会触发onDataChanged方法。 Is there anything I am missing? 有什么我想念的吗?

add a key-value in the data item map 在数据项映射中添加键值

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

and send again 并再次发送

yeah, you are missing some important meta-data tags 是的,你错过了一些重要的元数据标签

In wear manifest, add these tag to the WatchFaceService's service tag: 在Wear manifest中,将这些标记添加到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" />

And in your Companion manifest , just add only these lines to the CompanionConfigActivity's activity tag : 在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>

The code : 编码 :

com.example.package.CONFIG_FACE

must be same in both the manifests. 在两个清单中必须相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM