如何在android可穿戴设备中检测物理键点击?

时间:2016-12-07 05:21:47

标签: android wear-os

我正在使用索尼智能手表3,它有一个电源按钮,我想控制或说操纵。 我已经尝试过使用:

onKeyDown()
onKeyUp()
onKeyLongPress()

但没有成功,它甚至都没有检测到按键。

我也尝试过使用dispatchkeyevent(keyevent event)shouldOverrideKeyEvent(keyevent event)但又没有成功。 请指导。

1 个答案:

答案 0 :(得分:0)

您需要初始化并与GoogleApiClient建立联系。单击按钮后,您必须获取节点列表并向其发送message。最后一步是从应用程序的手机部分读取此消息,这可以通过注册正确的WearableListenerService来完成。请参阅下面的示例代码。

可穿戴应用

public class WearableButtonActivity extends Activity {

    private GoogleApiClient mGoogleApiClient;

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

        mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .build();
        mGoogleApiClient.connect();
    }

    public void onButtonClicked(View target) {
        if (mGoogleApiClient == null)
            return;

        final PendingResult<NodeApi.GetConnectedNodesResult> nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient);
        nodes.setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
            @Override
            public void onResult(NodeApi.GetConnectedNodesResult result) {
                final List<Node> nodes = result.getNodes();
                if (nodes != null) {
                    for (int i=0; i<nodes.size(); i++) {
                        final Node node = nodes.get(i);

                        // You can just send a message
                        Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), "/MESSAGE", null);

                        // or you may want to also check check for a result:
                        // final PendingResult<SendMessageResult> pendingSendMessageResult = Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), "/MESSAGE", null);
                        // pendingSendMessageResult.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
                        //      public void onResult(SendMessageResult sendMessageResult) {
                        //          if (sendMessageResult.getStatus().getStatusCode()==WearableStatusCodes.SUCCESS) {
                        //              // do something is successed
                        //          }
                        //      }
                        // });
                    }
                }
            }
        });
    }
}

<强>监听器:

 findViewById(R.id.button).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onButtonClicked(v);
        }
    });

手机应用

public class DataLayerListenerService extends WearableListenerService {

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        super.onMessageReceived(messageEvent);
        if("/MESSAGE".equals(messageEvent.getPath())) {
            // launch some Activity or do anything you like
        }
    }
}