Android Wear:在佩戴时点击通知操作按钮,在掌上电脑中启动活动

时间:2014-12-18 05:17:19

标签: android android-notifications wear-os

我想在掌上电脑应用中启动一项活动,并在用户点击服装中的通知操作按钮时发送一些数据。我使用下面的代码在磨损中构建通知。

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从该活动/服务发送消息到设备。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:10)

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

然而,您需要采取的方法是正确的:

  1. 为启动服务的操作创建PendingIntent(IntentService完美运行)
  2. 在该服务中:
    1. 连接到GoogleApiClient
    2. 如果找到已连接的设备,请使用OPEN_ON_PHONE_ANIMATION启动ConfirmationActivity(这会向您的用户显示您的应用正在其掌上电脑上打开内容的可见信号)
    3. 使用设置路径(例如notification/open
    4. 向连接的设备发送消息
  3. 在您的掌上电脑应用中,实施一个WearableListenerService,它会在后台收到您的消息:
    1. onMessageReceived()来电中,检查收到的信息路径,了解您设置的路径(即messageEvent.getPath().equals("notification/open")
    2. 如果匹配,则在掌上电脑上启动相应的活动。
  4. 当启动从未激活手持应用程序中的动态壁纸的表盘时,此方法用于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);
        }
    }