gpt4 book ai didi

Android WearableListenerService 必须调用两次才能接收消息

转载 作者:行者123 更新时间:2023-11-30 02:33:44 30 4
gpt4 key购买 nike

我正在通过我的手持设备从我的 Wear 向服务发送消息。我第一次单击手持设备上的按钮只是调用服务的 onCreate 方法,第二次服务也收到消息。我无法找出问题所在。

这是我的穿戴部分:

    @Override
public void onClick(View v)
{
final PendingResult<NodeApi.GetConnectedNodesResult> nodes1 = Wearable.NodeApi.getConnectedNodes(mApiClient);
nodes1.setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>()
{
@Override
public void onResult(NodeApi.GetConnectedNodesResult result)
{
for (int i = 0; i < result.getNodes().size(); i++)
{
Node node = result.getNodes().get(i);

PendingResult<MessageApi.SendMessageResult> messageResult = Wearable.MessageApi.sendMessage(mApiClient, node.getId(), "/OPEN;"
+ cnt, null);
messageResult.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>()
{
@Override
public void onResult(MessageApi.SendMessageResult sendMessageResult)
{
Status status = sendMessageResult.getStatus();
Log.d("", "##Status: " + status.toString());
if (status.getStatusCode() != WearableStatusCodes.SUCCESS)
{
//not getting there, always success
}
}
});
}
}
});
}

我有以下 WearableListenerService:

public class DataLayerListenerService extends WearableListenerService implements ConnectionCallbacks, OnConnectionFailedListener
{
private GoogleApiClient mApiClient;
private SharedPreferences prefs;

@Override
public void onCreate()
{
Log.d("", "##DataService created");
super.onCreate();
if (null == mApiClient)
{
mApiClient = new GoogleApiClient.Builder(this).addApi(Wearable.API).addConnectionCallbacks(this).addOnConnectionFailedListener(this)
.build();
}

if (!mApiClient.isConnected())
{
mApiClient.connect();
Log.d("", "##Api connecting");
}
}

@Override
public void onDestroy()
{
Log.d("", "##DataService destroyed");
if (null != mApiClient)
{
if (mApiClient.isConnected())
{
mApiClient.disconnect();
}
}
super.onDestroy();
}

@Override
public void onMessageReceived(MessageEvent messageEvent)
{
Log.d("", "##DataService received " + messageEvent.getPath());
super.onMessageReceived(messageEvent);
if (messageEvent.getPath().contains("/OPEN;"))
{
// only if called twice :(
}
}

@Override
public void onDataChanged(DataEventBuffer dataEvents)
{
Log.d("", "##DataService Data changed");
}


@Override
public void onConnectionFailed(ConnectionResult arg0)
{
}

@Override
public void onConnected(Bundle arg0)
{
}

@Override
public void onConnectionSuspended(int arg0)
{
}
}

也许我还应该提一下,之前服务被销毁时我只需要单击按钮两次,这发生在单击事件后几秒钟。所以..事实上这就是发生的事情:

wear click

service oncreate

wear click

service onMessageReceived

wear click

service onMessageReceived

//wait some seconds

service ondestroy

wear click

service oncreate

wear click

service onMessageReceived

希望这是清楚的:-)

提前致谢!

最佳答案

我想我可能已经解决了 Phillip Adam - 尝试在调用父类(super class)的 onMessageReceived() 方法中注释掉服务中的行。这对我来说是正确的。 :)

关于Android WearableListenerService 必须调用两次才能接收消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26911367/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com