onDataChanged()不会被调用Android上的磨损 [英] onDataChanged() not being called on android wear

查看:394
本文介绍了onDataChanged()不会被调用Android上的磨损的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用数据项通过发送一些私人关系把我的穿着,但我的穿着似乎从来没有收到任何信号,因为onDataChanged()不会被调用。我甚至设定一个时间标记,以确保无论何时被发送的数据始终是不同的。

有没有我必须安装应用程序到这两个设备,以得到它的工作一种特定的方式?我只是点击运行并选择我的电话,然后交换模块和做同样为我佩戴设备。

下面是从我我的手机上的主要活动code:

 公共类HomeActivity延伸活动{公共静态字符串标记=HomeActivity;
私人GoogleApiClient mGoogleApiClient;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.home_view);
    按钮mButton =(按钮)findViewById(R.id.send_button);
    mButton.setOnClickListener(新View.OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            送出数据();
        }
    });    mGoogleApiClient =新GoogleApiClient.Builder(本)
            .addConnectionCallbacks(新GoogleApiClient.ConnectionCallbacks(){
                @覆盖
                公共无效onConnected(束束){                }                @覆盖
                公共无效onConnectionSuspended(int i)以{                }
            })。addOnConnectionFailedListener(新GoogleApiClient.OnConnectionFailedListener(){
                @覆盖
                公共无效onConnectionFailed(ConnectionResult connectionResult){                }
            })。addApi(Wearable.API)
            。建立();
    mGoogleApiClient.connect();
}私人无效的SendData(){
    如果(mGoogleApiClient!= NULL){
        返回;
    }    最后PutDataMa prequest putRequest = Put​​DataMa prequest.create(/ SAMPLE);
    最终的数据映射图= putRequest.getDataMap();
    map.putInt(色,Color.RED);
    map.putLong(DATE,新的Date()的getTime());
    Wearable.DataApi.putDataItem(mGoogleApiClient,putRequest.asPutDataRequest());
}

}

这是我的穿戴code:

 公共类LayoutFaceService扩展CanvasWatchFaceService实现DataApi.DataListener {@覆盖
公共无效的onCreate(){
    super.onCreate();
    mGoogleApiClient =新GoogleApiClient.Builder(本)
            .addApi(Wearable.API)
            .addConnectionCallbacks(新GoogleApiClient.ConnectionCallbacks(){
                @覆盖
                公共无效onConnected(束束){
                    Log.i(TAG,连接);
                    Wearable.DataApi.addListener(mGoogleApiClient,LayoutFaceService.this);
                }                @覆盖
                公共无效onConnectionSuspended(int i)以{                }
            })
            .addOnConnectionFailedListener(新GoogleApiClient.OnConnectionFailedListener(){
                @覆盖
                公共无效onConnectionFailed(ConnectionResult connectionResult){                }
            })
            。建立();
    mGoogleApiClient.connect();
}
@覆盖
公共无效onDataChanged(DataEventBuffer dataEvents){
    对于(DataEvent事件:dataEvents){
        如果(event.getType()== DataEvent.TYPE_DELETED){
            Log.d(TAG的DataItem删除:+ event.getDataItem()getUri());
        }否则如果(event.getType()== DataEvent.TYPE_CHANGED){
            Log.d(TAG,DataItem的改变:+ event.getDataItem()getUri());
        }
    }}

和我的穿着清单:

 <使用特征的android:NAME =android.hardware.type.watch/><使用许可权的android:NAME =com.google.android.permission.PROVIDE_BACKGROUND/>
<使用许可权的android:NAME =android.permission.WAKE_LOCK/><应用
    机器人:allowBackup =真
    机器人:图标=@的mipmap / ic_launcher
    机器人:标签=@字符串/ APP_NAME
    机器人:主题=@安卓风格/ Theme.DeviceDefault>
    <服务
        机器人:名字=。LayoutFaceService
        机器人:标签=@字符串/ my_digital_name
        机器人:权限=android.permission.BIND_WALLPAPER>
        &所述;元数据
            机器人:名字=android.service.wallpaper
            机器人:资源=@ XML / watch_face/>
        &所述;元数据
            机器人:名字=。com.google.android.wearable.watchface preVIEW
            机器人:资源=@绘制/ preview_digital/>
        &所述;元数据
            机器人:名字=。com.google.android.wearable.watchface preview_circular
            机器人:资源=@绘制/ preview_digital_circular/>        &所述;意图滤光器>
            <作用机器人:名字=android.service.wallpaper.WallpaperService/>
            <类机器人:名字=com.google.android.wearable.watchface.category.WATCH_FACE/>
            <作用机器人:名字=com.google.android.gms.wearable.BIND_LISTENER/>
        &所述; /意图滤光器>
    < /服务>    &所述;元数据
        机器人:名字=com.google.android.gms.version
        机器人:值=@整数/ GOOGLE_PLAY_SERVICES_VERSION/>
< /用途>


解决方案

送出数据(),你的条件似乎是不正确的;所以你离开那里 mGoogleApiClient!= NULL 是真实的。地址,看看你是否能得到任何进一步的;可能还有其他的问题,但是这是第一个明显的例子。如果没有完全解决您的问题,然后确保你还包括手机上的体现在您的文章。

I'm trying to use Data Items to send a few strings through to my wear, but my wear never seems to receive any signal, because onDataChanged() is never called. I even set a time stamp to ensure the data is always different whenever it is sent.

Is there a specific way I have to install the app onto both devices to get it to work? I'm just clicking run and selecting my phone, then switching modules and doing the same for my wear device.

Here is the code from my main activity on my phone:

public class HomeActivity extends Activity{

public static String TAG = "HomeActivity";
private GoogleApiClient mGoogleApiClient;


@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_view);
    Button mButton = (Button) findViewById(R.id.send_button);
    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            sendData();
        }
    });

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle bundle) {

                }

                @Override
                public void onConnectionSuspended(int i) {

                }
            }).addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult connectionResult) {

                }
            }).addApi(Wearable.API)
            .build();
    mGoogleApiClient.connect();
}

private void sendData(){
    if (mGoogleApiClient!=null){
        return;
    }

    final PutDataMapRequest putRequest = PutDataMapRequest.create("/SAMPLE");
    final DataMap map = putRequest.getDataMap();
    map.putInt("color", Color.RED);
    map.putLong("date", new Date().getTime());
    Wearable.DataApi.putDataItem(mGoogleApiClient, putRequest.asPutDataRequest());
}

}

And here is the code on my wearable:

public class LayoutFaceService extends CanvasWatchFaceService implements DataApi.DataListener{

@Override
public void onCreate(){
    super.onCreate();
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle bundle) {
                    Log.i(TAG, "Connected");
                    Wearable.DataApi.addListener(mGoogleApiClient, LayoutFaceService.this);
                }

                @Override
                public void onConnectionSuspended(int i) {

                }
            })
            .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult connectionResult) {

                }
            })
            .build();
    mGoogleApiClient.connect();
}


@Override
public void onDataChanged(DataEventBuffer dataEvents) {
    for (DataEvent event : dataEvents) {
        if (event.getType() == DataEvent.TYPE_DELETED) {
            Log.d(TAG, "DataItem deleted: " + event.getDataItem().getUri());
        } else if (event.getType() == DataEvent.TYPE_CHANGED) {
            Log.d(TAG, "DataItem changed: " + event.getDataItem().getUri());
        }
    }

}

And my wear manifest :

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

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.DeviceDefault" >
    <service
        android:name=".LayoutFaceService"
        android:label="@string/my_digital_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" />
            <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>

解决方案

In sendData(), your conditional seems to be incorrect; mGoogleApiClient != null is true so you exit right there. Address that and see whether you can get any further; there might be other issues but that is the first obvious one. If that didn't completely fix your issue, then make sure you also include the manifest on your phone in your post.

这篇关于onDataChanged()不会被调用Android上的磨损的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆