我从手持设备向服务发送消息。 我第一次点击掌上电脑上的按钮只是调用服务的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)
{
}
}
也许我还应该提一下,当服务被破坏之前我必须点击两次按钮,这发生在点击事件后几秒钟。 所以......实际上就是这样:
点击
service oncreate
点击
service onMessageReceived
点击
service onMessageReceived
//等待几秒钟
service ondestroy
点击
service oncreate
点击
service onMessageReceived
希望这很清楚: - )
提前致谢!!
答案 0 :(得分:1)
我想我可能已经解决了它Phillip Adam - 尝试在onMessageReceived()方法中对你的服务中的行进行注释,该方法调用超类。这适合我。 :)
答案 1 :(得分:0)
我一直在搞这个,如果你仍然遇到第一次尝试没有触发的监听器服务的错误,我发现增加超时间隔会有所帮助。我将我的设置设置为10毫秒,但是将其设置为更大的值似乎有所帮助。
查看我为项目创建的WearableMainActivity.java in this gist中的代码。祝好运! :)