0

我目前正在开发 Android 可穿戴应用程序,但我对可穿戴应用程序还很陌生,我有一个小疑问需要纠正。

所以我的移动应用程序上有一个带按钮的屏幕,所以如果我按下按钮,那么我希望背景颜色会随着磨损而改变。

移动应用

mButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
       //How to send request to Wear Apps
    }
});

穿戴应用

final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub stub) {
            //How to receive request from Android mobile app
        }
    });

在此处输入图像描述

请浏览屏幕截图并检查我的项目继承人并建议我一些解决方案。

4

1 回答 1

2

活动/片段内部的移动端,创建连接

GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(getContext())
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle bundle) {
                    Log.d(LOG_TAG, "API client connected");
                }

                @Override
                public void onConnectionSuspended(int i) {
                    Log.d(LOG_TAG, "API client connection suspended");
                }
            })
            .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult connectionResult) {
                    Log.d(LOG_TAG, "API client connection failed");
                }
            })
            .addApi(Wearable.API)
            .build();
    mGoogleApiClient.connect();

使用 DataMap 发送数据项

    PutDataMapRequest putDataMapRequest = PutDataMapRequest.create("/app");
    putDataMapRequest.getDataMap().putString("DATA", data); // your data
    PutDataRequest putDataRequest = putDataMapRequest.asPutDataRequest();
    PendingResult<DataApi.DataItemResult> pendingResult = Wearable.DataApi.putDataItem(mGoogleApiClient, putDataRequest);
    pendingResult.setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
        @Override
        public void onResult(DataApi.DataItemResult dataItemResult) {
            if (dataItemResult.getStatus().isSuccess()){
                Log.d(LOG_TAG, "Sent weather data to watch");
            }
            else{
                Log.d(LOG_TAG, "Unable to send data to watch");
            }
            mGoogleApiClient.disconnect();
        }
    });

依赖项

compile 'com.google.android.support:wearable:1.1.0'
compile 'com.google.android.gms:play-services-wearable:7.8.0'

从 Wear 端,接收活动中的数据

GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(getBaseContext())
                .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                    @Override
                    public void onConnected(Bundle bundle) {
                        Log.d(LOG_TAG, "API client connected");
                        final DataApi.DataListener dataListener = new DataApi.DataListener() {
                            @Override
                            public void onDataChanged(DataEventBuffer dataEventBuffer) {
                                Log.e(LOG_TAG, "onDataChanged(): " + dataEventBuffer);
                                for (DataEvent event: dataEventBuffer){
                                    if (event.getType() == DataEvent.TYPE_CHANGED){
                                        DataItem dataItem = event.getDataItem();
                                        if (dataItem.getUri().getPath().equals("/app")){
                                            DataMap dataMap = DataMapItem.fromDataItem(dataItem).getDataMap();
                                            String data = dataMap.getString("DATA"); // You have received the data, do your stuff
                                            Log.d(LOG_TAG, "data " + data);
                                        }
                                    }
                                }
                            }
                        };
                        Wearable.DataApi.addListener(mGoogleApiClient, dataListener);
                    }

                    @Override
                    public void onConnectionSuspended(int i) {
                        Log.d(LOG_TAG, "API client connection suspended");
                    }
                })
                .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                        Log.d(LOG_TAG, "API client connection failed");
                    }
                })
                .addApi(Wearable.API)
                .build();
        mGoogleApiClient.connect();

依赖项

compile 'com.google.android.support:wearable:1.3.0'
compile 'com.google.android.gms:play-services-wearable:7.8.0'
于 2016-06-07T10:42:32.990 回答