我想在掌上电脑应用中启动一项活动,并在用户点击服装中的通知操作按钮时发送一些数据。我使用下面的代码在磨损中构建通知。
public Notification buildNotification(Context context) {
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
new Intent(context, MainActivity.class), 0);
return buildBasicNotification(context).extend(new Notification.WearableExtender()
.setContentIcon(R.drawable.content_icon_small)
.setContentIconGravity(Gravity.START))
.addAction(new Notification.Action(R.drawable.ic_launcher,
"Action A", pendingIntent))
.build();
}
是否可以仅通过待处理的意图在掌上电脑中启动活动,或者我们是否应该在点击操作按钮中启动服务/活动,并通过消息api从该活动/服务发送消息到设备。任何帮助将不胜感激!
答案 0 :(得分:10)
您的可穿戴应用程序与掌上电脑应用程序完全分开,唯一的通信路径是使用消息/数据API,因此遗憾的是无法通过可穿戴设备上生成的通知直接触发掌上电脑上的操作。
然而,您需要采取的方法是正确的:
OPEN_ON_PHONE_ANIMATION
启动ConfirmationActivity(这会向您的用户显示您的应用正在其掌上电脑上打开内容的可见信号)notification/open
)messageEvent.getPath().equals("notification/open")
)当启动从未激活手持应用程序中的动态壁纸的表盘时,此方法用于Muzei。可以在Github repository.
上找到涵盖这些部分的代码具体而言,步骤1和2可以在ActivateMuzeiIntentService中找到:
public static void showNotification(Context context) {
Notification.Builder builder = new Notification.Builder(context);
// Set up your notification as normal
// Create the launch intent, in this case setting it as the content action
Intent launchMuzeiIntent = new Intent(context,
ActivateMuzeiIntentService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0,
launchMuzeiIntent, 0);
builder.addAction(new Notification.Action.Builder(R.drawable.ic_open_on_phone,
context.getString(R.string.common_open_on_phone), pendingIntent)
.extend(new Notification.Action.WearableExtender()
.setAvailableOffline(false))
.build());
builder.extend(new Notification.WearableExtender()
.setContentAction(0));
// Send the notification with notificationManager.notify as usual
}
protected void onHandleIntent(Intent intent) {
// Open on Phone action
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.build();
ConnectionResult connectionResult =
googleApiClient.blockingConnect(30, TimeUnit.SECONDS);
if (!connectionResult.isSuccess()) {
Log.e(TAG, "Failed to connect to GoogleApiClient.");
return;
}
List<Node> nodes = Wearable.NodeApi.getConnectedNodes(googleApiClient)
.await().getNodes();
// Ensure there is a connected device
if (!nodes.isEmpty()) {
// Show the open on phone animation
Intent openOnPhoneIntent = new Intent(this, ConfirmationActivity.class);
openOnPhoneIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
openOnPhoneIntent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE,
ConfirmationActivity.OPEN_ON_PHONE_ANIMATION);
startActivity(openOnPhoneIntent);
// Clear the notification
NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
// Send the message to the phone to open Muzei
for (Node node : nodes) {
Wearable.MessageApi.sendMessage(googleApiClient, node.getId(),
"notification/open", null).await();
}
}
googleApiClient.disconnect();
}
然后手持设备端的第3步由MuzeiWearableListenerService处理:
public void onMessageReceived(MessageEvent messageEvent) {
String path = messageEvent.getPath();
if (path.equals("notification/open")) {
// In this case, we launch the launch intent for the application
// but it could be anything
PackageManager packageManager = getPackageManager();
Intent mainIntent = packageManager.getLaunchIntentForPackage(
getPackageName());
startActivity(mainIntent);
}
}