У меня есть простая программа, в которой я просто отправляю сообщение со своего телефона на эмулятор износа и наоборот, но приложение-функция onReceive никогда не вызывается на обоих устройствах. (и да, я связал свой телефон с эмулятором)
Это мой android код телефона:
public class MainActivity extends AppCompatActivity {
Button talkbutton;
TextView textview;
protected Handler myHandler;
int receivedMessageNumber = 1;
int sentMessageNumber = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
talkbutton = findViewById(R.id.talkButton);
textview = findViewById(R.id.textView);
//Create a message handler//
myHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
Bundle stuff = msg.getData();
messageText(stuff.getString("messageText"));
return true;
}
});
IntentFilter messageFilter = new IntentFilter(Intent.ACTION_SEND);
Receiver messageReceiver = new Receiver();
LocalBroadcastManager.getInstance(this).registerReceiver(messageReceiver, messageFilter);
}
public void messageText(String newinfo) {
if (newinfo.compareTo("") != 0) {
textview.append("\n" + newinfo);
}
}
//Define a nested class that extends BroadcastReceiver//
public class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String message = "I just received a message from the wearable " + receivedMessageNumber++;;
textview.setText(message);
}
}
public void talkClick(View v) {
String message = "Sending message.... ";
textview.setText(message);
new NewThread("/my_path", message).start();
}
//Use a Bundle to encapsulate the message//
public void sendmessage(String messageText) {
Bundle bundle = new Bundle();
bundle.putString("messageText", messageText);
Message msg = myHandler.obtainMessage();
msg.setData(bundle);
myHandler.sendMessage(msg);
}
class NewThread extends Thread {
String path;
String message;
//Constructor for sending information to the Data Layer//
NewThread(String p, String m) {
path = p;
message = m;
}
public void run() {
//Retrieve the connected devices
Task<List<Node>> wearableList =
Wearable.getNodeClient(getApplicationContext()).getConnectedNodes();
try {
List<Node> nodes = Tasks.await(wearableList);
for (Node node : nodes) {
Task<Integer> sendMessageTask =
//Send the message//
Wearable.getMessageClient(MainActivity.this).sendMessage(node.getId(), path, message.getBytes());
Integer result = Tasks.await(sendMessageTask);
sendmessage("I just sent the wearable a message " + sentMessageNumber++);
}
} catch (Exception exception) {
Toast.makeText(getApplicationContext(),exception.getMessage(),Toast.LENGTH_LONG);
}
}
}}
MessageService, который отправляет сообщение в приложение Wear OS:
public class MessageService extends WearableListenerService {
@Override
public void onMessageReceived(MessageEvent messageEvent) {
if (messageEvent.getPath().equals("/my_path")) {
final String message = new String(messageEvent.getData());
Intent messageIntent = new Intent();
messageIntent.setAction(Intent.ACTION_SEND);
messageIntent.putExtra("message", message);
LocalBroadcastManager.getInstance(this).sendBroadcast(messageIntent);
}
else {
super.onMessageReceived(messageEvent);
}
}
Поскольку функции получения и отправки в обоих приложениях очень идентичны, я думаю, что этого достаточно, чтобы показать вам код с устройства android, но если вам нравится здесь, это метод получения из приложения wearos:
IntentFilter newFilter = new IntentFilter(Intent.ACTION_SEND);
Receiver messageReceiver = new Receiver();
LocalBroadcastManager.getInstance(this).registerReceiver(messageReceiver, newFilter);
public class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Display the following when a new message is received//
String onMessageReceived = "I just received a message from the handheld " + receivedMessageNumber++;
textView.setText(onMessageReceived);
}
}