从推送通知IntentService使用GoogleApiClient发送数据到Android Wear [英] Using GoogleApiClient from Push Notification IntentService to Send Data to Android Wear

查看:201
本文介绍了从推送通知IntentService使用GoogleApiClient发送数据到Android Wear的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个后续问题... <一href="http://stackoverflow.com/questions/24784106/android-wear-bundled-notifications-and-background-images">Android穿捆绑声明和背景图像

我想创建一个获得在创建一个Android Wear GridViewPager布局时,一个新的推送通知的原因。

下面是我的code初始化一个GoogleApiClient连接时,一个新的消息进来,所以我就可以将数据发送到磨损的应用程序,然后创建GridView的寻呼机。

我的问题是,GoogleApiClient从来没有得到的连接。我已经成功地运行在SDK文件夹中的SynchonizedNotifications示例应用程序,所以我知道我的设备和手表正确配对。

下面是目前的code ...

 公共类GCMIntentService扩展IntentService实现GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {

    @覆盖
    保护无效onHandleIntent(上下文的背景下,意图意图){

        mGoogleApiClient =新GoogleApiClient.Builder(本)
            .addApi(Wearable.API)
            .addConnectionCallbacks(本)
            .addOnConnectionFailedListener(本)
            。建立();

        ConnectionResult connectionResult = mGoogleApiClient.blockingConnect(30,TimeUnit.SECONDS);

        //从信息中提取的有效载荷
        捆绑额外= intent.getExtras();
        如果(this.mGoogleApiClient.isConnected()){

           //发送一个简单的信息工程
           MessageApi.SendMessageResult结果= Wearable.MessageApi.sendMessage(mGoogleApiClient,
                node.getId(),路径,NULL).await();

            PutDataMa prequest putDataMa prequest = Put​​DataMa prequest.create(Constants.BOTH_PATH);
            。putDataMa prequest.getDataMap()putString(Constants.KEY_CONTENT,内容);
            。putDataMa prequest.getDataMap()putString(Constants.KEY_TITLE,标题);
            PutDataRequest请求= putDataMa prequest.asPutDataRequest();

            //将数据推送到这里穿的应用程序
            Wearable.DataApi.putDataItem(mGoogleApiClient,要求)
                .setResultCallback(新ResultCallback&其中; DataApi.DataItemResult&GT;(){
                    @覆盖
                    公共无效onResult(DataApi.DataItemResult dataItemResult){
                        如果(!dataItemResult.getStatus()。isSuccess()){
                            Log.e(TAG,无法设置数据,状态:+ dataItemResult.getStatus()的getStatus code());
                        }其他{
                            //到达这里,但磨损没有得到任何消息
                            Log.i(TAG,成功的响应收到磨损);
                        }
                        mGoogleApiClient.disconnect();
                    }
                });

        } 其他 {
            Log.e(TAG,没有谷歌API客户端连接);
        }
    }
}
 

解决方案

它看起来像你的数据是不是不断变化的。如果您使用的是DataItem的API的方式,不要把它当做了从设备发送数据到可穿戴(反之亦然)。相反,请记住,你只是操纵是跨设备共享的高速缓存:你的同步的数据,而不是的传输的是

这最终意味着你的听众不会触发,除非数据被更改。这就是为什么该方法被称为 onDataChanged()。底层API处理更新与缓存的 PutDataRequest 智能要高效得多。如果你要同步包含相同信息的有效载荷,你不会注意到什么。

那么试试这个:添加时间戳到您的有效载荷。也许像 putDataMa prequest.getDataMap()putLong(时间戳,System.currentTimeMillis的()); 您的有效载荷,每次会有所不同,你应该看到 onDataChanged()现在将触发。

是的,我花了一段时间来算出这个!

This is a follow up question to... Android Wear Bundled Notifications and Background Images

I'd like to create a Android Wear GridViewPager layout that get's created when a new push notification comes in.

Below is my code that initializes a GoogleApiClient connection when a new message comes in so I can then send data to the wear app which then creates the GridView Pager.

My problem is that the GoogleApiClient never gets a connection. I've successfully run the SynchonizedNotifications sample app in the sdk folder so I know my device and watch are paired correctly.

Below is current code...

public class GCMIntentService extends IntentService implements GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener{

    @Override
    protected void onHandleIntent(Context context, Intent intent) {

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

        ConnectionResult connectionResult =   mGoogleApiClient.blockingConnect(30, TimeUnit.SECONDS);

        // Extract the payload from the message
        Bundle extras = intent.getExtras();
        if (this.mGoogleApiClient.isConnected())        {

           // sending a simple message works
           MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(mGoogleApiClient,
                node.getId(), "path", null).await();

            PutDataMapRequest putDataMapRequest = PutDataMapRequest.create(Constants.BOTH_PATH);
            putDataMapRequest.getDataMap().putString(Constants.KEY_CONTENT, "content");
            putDataMapRequest.getDataMap().putString(Constants.KEY_TITLE, "title");
            PutDataRequest request = putDataMapRequest.asPutDataRequest();

            // push data to wear app here
            Wearable.DataApi.putDataItem(mGoogleApiClient, request)
                .setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
                    @Override
                    public void onResult(DataApi.DataItemResult dataItemResult) {
                        if (!dataItemResult.getStatus().isSuccess()) {
                            Log.e(TAG, "Failed to set the data, status: " + dataItemResult.getStatus().getStatusCode());
                        }else{
                            // get here, but no message received from wear
                            Log.i(TAG,"SUCCESSFUL RESPONSE RECEIVED FROM WEAR");
                        }
                        mGoogleApiClient.disconnect();
                    }
                });

        } else {
            Log.e(TAG, "no Google API Client connection");
        }
    }
}

解决方案

It looks like your data isn't ever changing. When you're using the DataItem API approach, don't think of it as sending data over from the device to the wearable (or vice versa). Instead, remember that you're just manipulating a cache that is shared across devices: you're syncing data, not transferring it.

This ultimately means that your listener won't trigger unless the data is changed. That's why the method is called onDataChanged(). The underlying API handles updating the cache with your PutDataRequest intelligently to be much more efficient. If you're syncing a payload that contains the same information, you won't notice anything.

So try this: add a timestamp to your payload. Maybe something like putDataMapRequest.getDataMap().putLong("timestamp", System.currentTimeMillis()); Your payload will be different each time, and you should see that onDataChanged() will now trigger.

Yeah, it took me a while to figure this out!

这篇关于从推送通知IntentService使用GoogleApiClient发送数据到Android Wear的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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