Android Wear:单击Wear中的通知操作按钮,在手持设备中启动活动
我想在手持应用程序中启动一项活动,并在用户单击应用程序中的通知操作按钮时发送一些数据。我正在使用下面的代码在wear中构建通知Android Wear:单击Wear中的通知操作按钮,在手持设备中启动活动,android,android-notifications,wear-os,Android,Android Notifications,Wear Os,我想在手持应用程序中启动一项活动,并在用户单击应用程序中的通知操作按钮时发送一些数据。我正在使用下面的代码在wear中构建通知 public Notification buildNotification(Context context) { PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.clas
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向设备发送消息。任何帮助都将不胜感激 您的可穿戴应用程序与手持应用程序完全分离,唯一的通信路径是使用消息/数据API,因此不幸的是,无法通过可穿戴应用程序上生成的通知直接触发手持设备上的操作 但是,您所采取的方法是正确的:
OPEN\u ON\u PHONE\u动画
启动(这会向您的用户显示一个可见的标志,表明您的应用程序正在他们的手持设备上打开某些内容)notification/open
)向连接的设备发送消息messageEvent.getPath().equals(“通知/打开”)
)您是从手持应用程序还是可穿戴应用程序创建通知?@ianhanniballake我是在可穿戴应用程序中创建通知非常感谢您的澄清。
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();
}
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);
}
}