Android 将数据从活动发送到WearableListenerService

Android 将数据从活动发送到WearableListenerService,android,wear-os,android-wear-data-api,Android,Wear Os,Android Wear Data Api,当我读到有关活动和服务之间的通信时,我发现我们可以使用 伊宾德 信使 艾德尔 我对前两个感兴趣。因此,当我尝试在活动和可穿戴ListenerService之间实现此功能时,我需要覆盖onBind功能 但是,我得到一个编译器错误,说它 无法覆盖最终方法“onBind” 当我使用正常的服务时,我不会遇到这样的错误。所以 1.这是否意味着我们不能使用IBinder或Messenger方法从活动与可穿戴监听器服务进行通信 2.如果是这样,那么从活动(或者从活动中调用该服务的公共方法)向Wearabl

当我读到有关
活动
服务
之间的通信时,我发现我们可以使用

  • 伊宾德
  • 信使
  • 艾德尔
我对前两个感兴趣。因此,当我尝试在
活动
可穿戴ListenerService
之间实现此功能时,我需要覆盖
onBind
功能

但是,我得到一个编译器错误,说它

无法覆盖最终方法“onBind”

当我使用正常的
服务时,我不会遇到这样的错误。所以

1.这是否意味着我们不能使用
IBinder
Messenger
方法从
活动
可穿戴监听器服务
进行通信


2.如果是这样,那么从
活动
(或者从活动中调用该服务的公共方法)向
WearableListenerService
传递消息的下一个最佳方式是什么?

经过一些挖掘,我找到了解决方案。希望它能帮助别人

我们可以使用
Wearable.MessageApi
函数将活动中的消息发送到
WearableListenerService
。 当
活动
WearableListenerService
位于同一节点(设备)上时,我们需要获取本地节点的实例(发送消息的当前节点),以发送消息,如下所示

NodeApi.GetLocalNodeResult nodes = Wearable.NodeApi.getLocalNode(mGoogleApiClient).await();
而不是

NodeApi.GetConnectedNodesResult nodes  = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
用于获取连接到手机的其他设备(如wear)的列表

因此,我能够成功地从我的活动向WearableListenerService发送消息,如下所示

活动代码

服务代码


@pskink我指的是将数据从活动发送到“WearableListenerService”(或从活动触发“WearableListenerService”中的公共功能)。但是链接中的数据层API示例提供了监听“WearableListenerService”事件的详细信息,而不是从未使用过的另一种方式,但不是
onMessageReceived()
?不知道,但这可能会有所帮助:我偶然发现了同样的问题。根据pskink的link,localbroadcast接收器是服务和活动之间通信的方式。这也在官方文件中有所说明。由于您可以使用活动中的GoogleAppClient在设备之间进行通信,因此不需要从活动到服务进行通信。
public class PhoneActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{

    private static final String TAG = "PhoneActivity";

    public static final String CONFIG_START = "config/start";
    public static final String CONFIG_STOP= "config/stop"

    Intent intent;
    TextView txtview;

    GoogleApiClient mGoogleApiClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone);

        if(null == mGoogleApiClient) {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(Wearable.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();
            Log.v(TAG, "GoogleApiClient created");
        }

        if(!mGoogleApiClient.isConnected()){
            mGoogleApiClient.connect();
            Log.v(TAG, "Connecting to GoogleApiClient..");
        }

        startService(new Intent(this, PhoneService.class));
    }

    @Override
    public void onConnectionSuspended(int cause) {
        Log.v(TAG,"onConnectionSuspended called");
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.v(TAG,"onConnectionFailed called");
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        Log.v(TAG,"onConnected called");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.v(TAG, "onStart called");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.phone, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_start_) {
            new SendActivityPhoneMessage(CONFIG_START,"").start();
        }else if (id == R.id.action__stop) {
            new SendActivityPhoneMessage(CONFIG_STOP,"").start();
        }else if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    class SendActivityPhoneMessage extends Thread {
        String path;
        String message;

        // Constructor to send a message to the data layer
        SendActivityPhoneMessage(String p, String msg) {
            path = p;
            message = msg;
        }

        public void run() {
            NodeApi.GetLocalNodeResult nodes = Wearable.NodeApi.getLocalNode(mGoogleApiClient).await();
            Node node = nodes.getNode();
            Log.v(TAG, "Activity Node is : "+node.getId()+ " - " + node.getDisplayName());
            MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), path, message.getBytes()).await();
            if (result.getStatus().isSuccess()) {
                Log.v(TAG, "Activity Message: {" + message + "} sent to: " + node.getDisplayName());
            }
            else {
                // Log an error
                Log.v(TAG, "ERROR: failed to send Activity Message");
            }
        }
    }
}
public class PhoneService extends WearableListenerService implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{

    private static final String TAG = "PhoneService";

    public static final String CONFIG_START = "config/start";
    public static final String CONFIG_STOP = "config/stop";

    GoogleApiClient mGoogleApiClient;

    public PhoneService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.v(TAG, "Created");

        if(null == mGoogleApiClient) {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(Wearable.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();
            Log.v(TAG, "GoogleApiClient created");
        }

        if(!mGoogleApiClient.isConnected()){
            mGoogleApiClient.connect();
            Log.v(TAG, "Connecting to GoogleApiClient..");
        }
    }

    @Override
    public void onDestroy() {

        Log.v(TAG, "Destroyed");

        if(null != mGoogleApiClient){
            if(mGoogleApiClient.isConnected()){
                mGoogleApiClient.disconnect();
                Log.v(TAG, "GoogleApiClient disconnected");
            }
        }

        super.onDestroy();
    }

    @Override
    public void onConnectionSuspended(int cause) {
        Log.v(TAG,"onConnectionSuspended called");
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.v(TAG,"onConnectionFailed called");
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        Log.v(TAG,"onConnected called");

    }

    @Override
    public void onDataChanged(DataEventBuffer dataEvents) {
        super.onDataChanged(dataEvents);
        Log.v(TAG, "Data Changed");
    }

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        super.onMessageReceived(messageEvent);
        if(messageEvent.getPath().equals(CONFIG_START)){
            //do something here
        }else if(messageEvent.getPath().equals(CONFIG_STOP)){
            //do something here
        }
    }

    @Override
    public void onPeerConnected(Node peer) {
        super.onPeerConnected(peer);
        Log.v(TAG, "Peer Connected " + peer.getDisplayName());
    }

    @Override
    public void onPeerDisconnected(Node peer) {
        super.onPeerDisconnected(peer);
        Log.v(TAG, "Peer Disconnected " + peer.getDisplayName());
    }
}