gpt4 book ai didi

安卓磨损 : launching an activty in the handheld on clicking notification action button in wear

转载 作者:太空狗 更新时间:2023-10-29 15:41:23 26 4
gpt4 key购买 nike

我想在手持应用程序中启动一个 Activity ,并在用户点击穿戴中的通知操作按钮时发送一些数据。我正在使用以下代码在穿戴中构建通知。

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();
}

是否可以仅通过未决 Intent 在手持设备中启动 Activity ,或者我们是否应该在单击操作按钮时在穿戴设备中启动服务/Activity ,并从该 Activity/服务通过消息 api 向设备发送消息。任何帮助将不胜感激!

最佳答案

您的可穿戴设备应用与手持设备应用完全分开,唯一的通信路径是使用消息/数据 API,因此很遗憾,无法根据可穿戴设备上生成的通知直接触发手持设备上的操作。

您在需要采取的方法方面是正确的,但是:

  1. 为启动服务的操作创建一个 PendingIntent(IntentService 完美运行)
  2. 在该服务中:
    1. 连接到 GoogleApiClient
    2. 如果找到连接的设备,启动 ConfirmationActivity使用 OPEN_ON_PHONE_ANIMATION(这会向您的用户显示一个可见标志,表明您的应用正在他们的手持设备上打开某些内容)
    3. 使用设置的路径(例如,notification/open)向连接的设备发送消息
  3. 在您的手持应用程序中,实现 WearableListenerService它将在后台收到您的消息:
    1. onMessageReceived()调用,检查收到消息的路径是否为您设置的路径(即 messageEvent.getPath().equals("notification/open"))
    2. 如果匹配,则在您的手持设备上启动适当的 Activity 。

此方法用于 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);
}
}

关于安卓磨损 : launching an activty in the handheld on clicking notification action button in wear,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27539672/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com