我是 Android Wear 编程的新手,我在开发这个应用程序时正在学习。
它只是一个媒体控制器——这样用户可以在手表上按下播放/暂停,播放视频的手机接收并处理它。
到目前为止我做了什么:
我已设置 Android Wear 应用程序,以便我可以从移动应用程序接收通知。
Intent notificationIntent = new Intent(this, NotificationActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.extend(new NotificationCompat.WearableExtender()
.setContentIcon(R.drawable.ic_launcher) .setCustomSizePreset(NotificationCompat.WearableExtender.SIZE_FULL_SCREEN)
.setDisplayIntent(pendingIntent)
.addAction(new NotificationCompat.Action.Builder(R.drawable.ic_launcher, "Hej", pendingIntent).build())
.setContentAction(0));
((NotificationManager) getSystemService(NOTIFICATION_SERVICE))
.notify(NOTIFICATION_ID, notificationBuilder.build());
上面的代码是我用来在手表上显示我的远程卡的代码。但是,我想要实现的是,当按下此按钮时,它应该向手机发送一条消息 BACK。
现在我的问题是:
- 我只能将新意图设置为操作。这真的是将一条简单消息(如字符串或整数)发送回手机的正确方法吗?就像一个只发送消息然后破坏的活动?如果是这样,我如何将 API 放入该活动中,以便将消息发送到手机?
- 如何发送此消息并在手机上接收?我在指南中对此一无所获......我已经看到迹象表明我可以在 Wear 设备发送消息时在手机上启动活动,但这绝对不是我想要做的,因为我在那里有一个正在运行的活动。 ..
谢谢!