问题描述
我正在为 android 编写一个应用程序,它与设备通信并返回到磨损.这是onclick on wear的代码.
message1Button.setOnClickListener(new View.OnClickListener() {@覆盖public void onClick(View v) {Wearable.MessageApi.sendMessage(apiClient, remoteNodeId, MESSAGE1_PATH, null).setResultCallback(new ResultCallback() {@覆盖public void onResult(MessageApi.SendMessageResult sendMessageResult) {Intent intent = new Intent(getApplicationContext(), ConfirmationActivity.class);如果 (sendMessageResult.getStatus().isSuccess()) {intent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE, ConfirmationActivity.SUCCESS_ANIMATION);intent.putExtra(ConfirmationActivity.EXTRA_MESSAGE, getString(R.string.message1_sent));} 别的 {intent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE, ConfirmationActivity.FAILURE_ANIMATION);intent.putExtra(ConfirmationActivity.EXTRA_MESSAGE, getString(R.string.error_message1));}开始活动(意图);}});
以下是在手持设备上收到的代码.
public void onMessageReceived(MessageEvent messageEvent) {如果 (messageEvent.getPath().equals(MESSAGE1_PATH)) {handler.post(new Runnable() {@覆盖公共无效运行(){receivedMessagesEditText.append("\n" + getString(R.string.received_message1));String serverURL = "一个有效的 URL";尝试 {ArrayListlist=new ArrayList();list.add(0,serverURL);list.add(1,"SELECT DISTINCT ACCOUNTNO FROM CUSTOMR_WISE_WALLET_POINTS ORDER BY ACCOUNTNO");jsonContent= new LongOperation().execute(list).get();} catch (InterruptedException e) {Log.e("错误",e.getMessage());e.printStackTrace();} catch (ExecutionException e) {e.printStackTrace();}Log.d("json 的值",jsonContent);}});Wearable.MessageApi.sendMessage(apiClient, remoteNodeId, MESSAGE4_PATH, jsonContent.getBytes()).setResultCallback(new ResultCallback() {@覆盖public void onResult(MessageApi.SendMessageResult sendMessageResult) {如果 (sendMessageResult.getStatus().isSuccess())Toast.makeText(getApplication(), "Message 4 sent", Toast.LENGTH_SHORT).show();别的Toast.makeText(getApplication(), getString(R.string.error_message1), Toast.LENGTH_SHORT).show();}});} else if (messageEvent.getPath().equals(MESSAGE2_PATH)) {handler.post(new Runnable() {@覆盖公共无效运行(){receivedMessagesEditText.append("\n" + getString(R.string.received_message2));}});}}
手持设备显示消息 4 已发送"吐司.但是从来没有触发过磨损上的消息侦听器.其代码是.
messageListener = new MessageApi.MessageListener() {@覆盖public void onMessageReceived(final MessageEvent messageEvent) {Log.d("内部","收到消息");如果 (messageEvent.getPath().equals(MESSAGE1_PATH)) {handler.post(new Runnable() {@覆盖公共无效运行(){receivedMessagesEditText.append("\n" + getString(R.string.received_message1));/*发送另一个请求的代码*/Wearable.MessageApi.sendMessage(apiClient, remoteNodeId, MESSAGE3_PATH, "Hello".getBytes()).setResultCallback(new ResultCallback() {@覆盖public void onResult(MessageApi.SendMessageResult sendMessageResult) {Intent intent = new Intent(getApplicationContext(), ConfirmationActivity.class);如果 (sendMessageResult.getStatus().isSuccess()) {intent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE, ConfirmationActivity.SUCCESS_ANIMATION);intent.putExtra(ConfirmationActivity.EXTRA_MESSAGE, "Message 3 sent");} 别的 {intent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE, ConfirmationActivity.FAILURE_ANIMATION);intent.putExtra(ConfirmationActivity.EXTRA_MESSAGE, getString(R.string.error_message2));}开始活动(意图);}});}});} else if (messageEvent.getPath().equals(MESSAGE2_PATH)) {handler.post(new Runnable() {@覆盖公共无效运行(){receivedMessagesEditText.append("\n" + getString(R.string.received_message2));}});}否则 if(messageEvent.getPath().equals(MESSAGE4_PATH)){handler.post(new Runnable() {@覆盖公共无效运行(){receivedMessagesEditText.append("\n" + getString(R.string.received_message1));/*发送另一个请求的代码*///String jsonInString=new String(messageEvent.getData());Log.d("Inside Message 4","Inside");String[] items = new String[]{"选择用户", "2", "三个"};ArrayAdapter适配器 = 新 ArrayAdapter(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, items);dropdown.setAdapter(适配器);message2Button.setEnabled(true);}});}}};
总而言之,我可以从 Wear 发送消息并触发在手柄上接收到的消息.从手持设备成功发送消息,但从 messagelistener 范围内调用时,不会在佩戴上触发收到的 on 消息.我检查了应用程序 ID,它们是相同的.我还测试了通过使用手持设备的 button2 触发而在磨损上收到的消息.它工作正常.提前致谢
MessageApi 不保证即使返回成功的结果代码也会收到消息.在此处查看文档.
注意:成功的结果代码并不能保证消息的传递.如果您的应用需要数据可靠性,请使用 DataItem对象或 ChannelApi 类之间发送数据设备.
I am writing an app for android which communicates to the device and back to the wear. This is the code of onclick on wear.
message1Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Wearable.MessageApi.sendMessage(apiClient, remoteNodeId, MESSAGE1_PATH, null).setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
@Override
public void onResult(MessageApi.SendMessageResult sendMessageResult) {
Intent intent = new Intent(getApplicationContext(), ConfirmationActivity.class);
if (sendMessageResult.getStatus().isSuccess()) {
intent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE, ConfirmationActivity.SUCCESS_ANIMATION);
intent.putExtra(ConfirmationActivity.EXTRA_MESSAGE, getString(R.string.message1_sent));
} else {
intent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE, ConfirmationActivity.FAILURE_ANIMATION);
intent.putExtra(ConfirmationActivity.EXTRA_MESSAGE, getString(R.string.error_message1));
}
startActivity(intent);
}
});
public void onMessageReceived(MessageEvent messageEvent) {
if (messageEvent.getPath().equals(MESSAGE1_PATH)) {
handler.post(new Runnable() {
@Override
public void run() {
receivedMessagesEditText.append("\n" + getString(R.string.received_message1));
String serverURL = "A Working URL";
try {
ArrayList<String> list=new ArrayList<String>();
list.add(0,serverURL);
list.add(1,"SELECT DISTINCT ACCOUNTNO FROM CUSTOMR_WISE_WALLET_POINTS ORDER BY ACCOUNTNO");
jsonContent= new LongOperation().execute(list).get();
} catch (InterruptedException e) {
Log.e("Error",e.getMessage());
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
Log.d("Value of json",jsonContent);
}
});
Wearable.MessageApi.sendMessage(apiClient, remoteNodeId, MESSAGE4_PATH, jsonContent.getBytes()).setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
@Override
public void onResult(MessageApi.SendMessageResult sendMessageResult) {
if (sendMessageResult.getStatus().isSuccess())
Toast.makeText(getApplication(), "Message 4 sent", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplication(), getString(R.string.error_message1), Toast.LENGTH_SHORT).show();
}
});
} else if (messageEvent.getPath().equals(MESSAGE2_PATH)) {
handler.post(new Runnable() {
@Override
public void run() {
receivedMessagesEditText.append("\n" + getString(R.string.received_message2));
}
});
}
}
The handheld device shows "Message 4 sent" toast. but the message listener on the wear is never triggered.The code for which is.
messageListener = new MessageApi.MessageListener() {
@Override
public void onMessageReceived(final MessageEvent messageEvent) {
Log.d("Inside","On message recieved");
if (messageEvent.getPath().equals(MESSAGE1_PATH)) {
handler.post(new Runnable() {
@Override
public void run() {
receivedMessagesEditText.append("\n" + getString(R.string.received_message1));
/*COde to send another request*/
Wearable.MessageApi.sendMessage(apiClient, remoteNodeId, MESSAGE3_PATH, "Hello".getBytes()).setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
@Override
public void onResult(MessageApi.SendMessageResult sendMessageResult) {
Intent intent = new Intent(getApplicationContext(), ConfirmationActivity.class);
if (sendMessageResult.getStatus().isSuccess()) {
intent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE, ConfirmationActivity.SUCCESS_ANIMATION);
intent.putExtra(ConfirmationActivity.EXTRA_MESSAGE, "Message 3 sent");
} else {
intent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE, ConfirmationActivity.FAILURE_ANIMATION);
intent.putExtra(ConfirmationActivity.EXTRA_MESSAGE, getString(R.string.error_message2));
}
startActivity(intent);
}
});
}
});
} else if (messageEvent.getPath().equals(MESSAGE2_PATH)) {
handler.post(new Runnable() {
@Override
public void run() {
receivedMessagesEditText.append("\n" + getString(R.string.received_message2));
}
});
}
else if(messageEvent.getPath().equals(MESSAGE4_PATH)){
handler.post(new Runnable() {
@Override
public void run() {
receivedMessagesEditText.append("\n" + getString(R.string.received_message1));
/*COde to send another request*/
// String jsonInString=new String(messageEvent.getData());
Log.d("Inside Message 4","Inside");
String[] items = new String[]{"Select User", "2", "three"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, items);
dropdown.setAdapter(adapter);
message2Button.setEnabled(true);
}
});
}
}
};
To summarize, I can send the message from wear and trigger messagerecieved on the handhled. The message is successfully sent from handheld but the on message received is not triggered on the wear when called from within the scope of messagelistener . I checked the app Id , they are same.I also tested on message recieved on the wear by triggering with button2 from handheld.It works fine. Thanks in advance
MessageApi does not guarantee that a message will be received even a successful result code returned. Check the document here.
这篇关于向android设备发送消息,该设备将消息发送回androidwear,但在收到时永远不会在wear时触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!