按钮单击android的磨损 [英] Button click in android wear

查看:103
本文介绍了按钮单击android的磨损的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已创建了一个小按钮(快捷方式到活动中我的手机应用程序)的Andr​​oid磨损的活动。是否有可能一个包请求发到我的主要的应用程序?

I have created android wear activity with a small button (A shortcut to activity in my mobile app). Is it possible to send a bundle request to my main app?

如果因此如何实现点击监听器?

If so how to implement the click listener?

谢谢!

推荐答案

这可以通过提到MessageApi来完成:的 http://developer.android.com/training/wearables/data-layer/messages.html
你需要初始化并与GoogleApiClient。一旦点击按钮你必须得到节点的列表以及将消息发送到它们。最后一步是将读此消息氟里昂应用程序的电话部分,这可以通过注册适当WearableListenerService来完成。请参阅下面的示例code。

This can be done using mentioned MessageApi: http://developer.android.com/training/wearables/data-layer/messages.html
You need to initialize and connect with GoogleApiClient. Once you clicks the button you have to get list of nodes and send a message to them. The last step is to read this message fron phone part of app, this can be done by registering proper WearableListenerService. Please see the sample code below.

耐磨应用程序的一部分:

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
                        //          }
                        //      }
                        // });
                    }
                }
            }
        });
    }
}

和你的RES /布局/ wearable_button_activity.xml文件攻击onButtonClick方法的按钮:

and attack onButtonClick method to your button in your res/layout/wearable_button_activity.xml file:

<Button android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click me!"
    android:layout_gravity="center"
    android:onClick="onButtonClicked" />

刚才设置OnClickListener从code,如果你喜欢这种方式:

OR just set OnClickListener from code if you like that way:

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

手机应用程序的一部分:
然后声明DataLayerListenerService在YOUT电话的​​清单:

Phone part of app:
Then declare DataLayerListenerService on yout phone's manifest:

<service android:name=".DataLayerListenerService" >
    <intent-filter>
        <action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
    </intent-filter>
</service>

DataLayerListenerService类:

DataLayerListenerService class:

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
        }
    }
}



重要提示:您的应用程序的两个部分需要有相同的包名称的相互联系



IMPORTANT: Both parts of your app need to have the same package name to contact with each other.

这篇关于按钮单击android的磨损的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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