Android Wearable.API 已被弃用,现在推荐使用 Google Play 服务中的 Wear OS API
在您的 app 的 build.gradle 文件中添加以下依赖项:
dependencies {
implementation 'com.google.android.gms:play-services-wearable:17.1.0'
}
在您的代码中,您可以使用 Wearable API。以下是一些基本的用法示例:
import com.google.android.gms.wearable.Wearable;
Wearable wear = Wearable.get(context);
Node connectedNode = wear.getConnectedNodes().await().get(0);
import com.google.android.gms.wearable.MessageApi;
import com.google.android.gms.wearable.Wearable;
Wearable.MessageApi.sendMessage(
googleApiClient, node.getId(), path, message.getBytes()).setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
@Override
public void onResult(@NonNull MessageApi.SendMessageResult sendMessageResult) {
if (sendMessageResult.getStatus().isSuccess()) {
Log.d(TAG, "Message sent successfully");
} else {
Log.d(TAG, "Failed to send Message");
}
}
});
import com.google.android.gms.wearable.MessageApi;
import com.google.android.gms.wearable.WearableListenerService;
public class MyWearableListenerService extends WearableListenerService {
@Override
public void onMessageReceived(MessageEvent messageEvent) {
if (messageEvent.getPath().equals("/my-path")) {
String message = new String(messageEvent.getData(), StandardCharsets.UTF_8);
// 处理接收到的消息
}
}
}
<service
android:name=".MyWearableListenerService"
android:exported="true">
<intent-filter>
<action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
</intent-filter>
</service>
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云