如何启动从移动机器人磨损活动 [英] How to launch android wear activity from mobile

查看:103
本文介绍了如何启动从移动机器人磨损活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在做一个项目,我需要在移动的一个按钮来启动一个活动上的手表。我一直在经历SDK中的数据层样品,但不能让它的工作。我设置了一个可穿戴式监听器的服务类,但是它没有拿起任何消息。该服务被添加到清单,但它仍不能工作。我也有其他的服务也和我想我可能有太多的服务。

I have been working on a project where I need a button on a mobile to start up an activity on the watch. I have been going through the data layer sample in the sdk but can not get it working. I set up a wearable listener service class, but it is not picking up any messages. The service is added to the manifest but it is still not working. I do have other services too and I am thinking I might have too many services.

在磨损手表,并在活动必须运行,以便它启动另一个活动?我想手表,直到收到一个消息运行什么都没有,这可能吗?

On the Wear watch, does an activity have to be running in order for it to start another activity? I want the watch to run nothing until it receives a message, is this possible?

此外,什么应该我是否有磨损模块编辑配置设置是什么? (例如,不要启动活动,启动默认或启动的活动),我只是想在收到邮件时可穿戴启动。

Also, what should my edit configuration settings for wear module be? (eg, do not launch activity, launch default or launch from activity) I just want the wearable to boot up when a message is received.

如果任何人都可以在正确的方向指向我,那将是巨大的AP preciated。

If anybody can point me in the right direction, it would be hugely appreciated.

感谢。

推荐答案

移动

访问耐磨数据层

MainActivity.java

public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    mGoogleApiClient.connect();
}

@Override
public void onConnected(Bundle bundle) {
    Log.d("GoogleApi", "onConnected: " + bundle);
}

@Override
public void onConnectionSuspended(int i) {
    Log.d("GoogleApi", "onConnectionSuspended: " + i);
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.d("GoogleApi", "onConnectionFailed: " + connectionResult);
}

GetConnectedNodes

GetConnectedNodes

Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
                @Override
                public void onResult(NodeApi.GetConnectedNodesResult getConnectedNodesResult) {
                    for (Node node : getConnectedNodesResult.getNodes()) {
                        sendMessage(node.getId());
                    }
                }
            });

SendMessage函数

SendMessage

public static final String START_ACTIVITY_PATH = "/start/MainActivity";


private void sendMessage(String node) {
        Wearable.MessageApi.sendMessage(mGoogleApiClient , node , START_ACTIVITY_PATH , new byte[0]).setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
            @Override
            public void onResult(MessageApi.SendMessageResult sendMessageResult) {
                if (!sendMessageResult.getStatus().isSuccess()) {
                    Log.e("GoogleApi", "Failed to send message with status code: "
                            + sendMessageResult.getStatus().getStatusCode());
                }
            }
        });
    }

戴上

实现消息监听器

WearDataLayerListenerService.java

public class WearDataLayerListenerService extends WearableListenerService {
public static final String START_ACTIVITY_PATH = "/start/MainActivity";
@Override
public void onMessageReceived(MessageEvent messageEvent) {
    super.onMessageReceived(messageEvent);
    if(messageEvent.getPath().equals(START_ACTIVITY_PATH)){
        Intent intent = new Intent(this , MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}}

添加监听服务的体现

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

这篇关于如何启动从移动机器人磨损活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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