Android Wear:在点击穿通知操作按钮启动在手持设备的activty [英] Android Wear : launching an activty in the handheld on clicking notification action button in wear

查看:188
本文介绍了Android Wear:在点击穿通知操作按钮启动在手持设备的activty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要开始在手持式应用程序的活动,并发送一些数据,当用户点击穿通知操作按钮。我使用低于code打造的磨损通知。

 公示buildNotification(上下文的背景下){
        PendingIntent pendingIntent = PendingIntent.getActivity(上下文,0,
                新意图(上下文,MainActivity.class),0);

        返回buildBasicNotification(上下文).extend(新Notification.WearableExtender()
                .setContentIcon(R.drawable.content_icon_small)
                .setContentIconGravity(Gravity.START))
                .addAction(新Notification.Action(R.drawable.ic_launcher,
                        行动A,pendingIntent))


                。建立();
    }
 

是否有可能通过悬而未决的意图,开始在掌上电脑上的活动只还是应该启动一个服务/活动的磨损单击动作按钮并从通过消息的API,活动/服务发送消息到设备。任何帮助将是AP preciated!

解决方案

您可穿戴的应用程序是从手持设备的应用程序完全独立的,与使用消息/数据API,因此不幸的是没有办法直接触发的唯一通信路径是从上可穿戴生成一个通知手持操作。

您是一个需要采取的方法正确,但是:

  1. 创建一个PendingIntent为您的操作启动服务(一个 IntentService 完美的作品)
  2. 在该服务:
    1. 连接到GoogleApiClient
    2. 如果找到一个连接的设备,开始了<一href="http://developer.android.com/training/wearables/ui/confirm.html#show-confirmation">ConfirmationActivity使用 OPEN_ON_PHONE_ANIMATION (这将显示一个明显的迹象,以你的用户,你的应用程序是在他们的掌上电脑打开的东西)
    3. 使用设置的路径将消息发送给连接的设备(例如,的通知/打开
  3. 在掌上电脑上的应用程序,实施<一个href="http://developer.android.com/reference/com/google/android/gms/wearable/WearableListenerService.html">WearableListenerService这将收到您的消息的背景:
    1. 在<一个href="http://developer.android.com/reference/com/google/android/gms/wearable/WearableListenerService.html#onMessageReceived(com.google.android.gms.wearable.MessageEvent)">onMessageReceived()调用,检查接收到的信息的路径为您设置的路径(即 messageEvent.getPath()。等于(通知/开)
    2. 如果匹配,则启动相应的活动在掌上电脑上。

开始表盘时,这种方法用在 Muzei 有从未被激活,在手持设备的应用程序的动态壁纸。 code覆盖每一部分都可以在 Github的资料库中找到。

具体而言,步骤1和2可以在<一个中找到href="https://github.com/romannurik/muzei/blob/master/wearable/src/main/java/com/google/android/apps/muzei/ActivateMuzeiIntentService.java">ActivateMuzeiIntentService:

 公共静态无效showNotification(上下文的背景下){
    Notification.Builder建设者=新Notification.Builder(上下文);
    //设置您的通知正常

    //创建发射意图,在这种情况下,将其设置为内容的动作
    意图launchMuzeiIntent =新的意图(背景下,
        ActivateMuzeiIntentService.class);
    PendingIntent pendingIntent = PendingIntent.getService(上下文,0,
        launchMuzeiIntent,0);
    builder.addAction(新Notification.Action.Builder(R.drawable.ic_open_on_phone,
            context.getString(R.string.common_open_on_phone),pendingIntent)
            .extend(新Notification.Action.WearableExtender()
                    .setAvailableOffline(假))
            。建立());
    builder.extend(新Notification.WearableExtender()
            .setContentAction(0));

    //发送通知,其中notificationManager.notify像往常一样
}

保护无效onHandleIntent(意向意图){
    //打开手机上的行动
    GoogleApiClient googleApiClient =新GoogleApiClient.Builder(本)
            .addApi(Wearable.API)
            。建立();
    ConnectionResult connectionResult =
            googleApiClient.blockingConnect(30,TimeUnit.SECONDS);
    如果(!connectionResult.isSuccess()){
        Log.e(TAG,无法连接到GoogleApiClient。);
        返回;
    }
    名单&LT;节点&GT;节点= Wearable.NodeApi.getConnectedNodes(googleApiClient)
        .await()getNodes()。
    //确保有一个连接的设备
    如果(!nodes.isEmpty()){
        //显示在手机动漫的开
        意图openOnPhoneIntent =新的意图(这一点,ConfirmationActivity.class);
        openOnPhoneIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        openOnPhoneIntent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE,
                ConfirmationActivity.OPEN_ON_PHONE_ANIMATION);
        startActivity(openOnPhoneIntent);
        //清除通知
        NotificationManager notificationManager =(NotificationManager)
                getSystemService(NOTIFICATION_SERVICE);
        notificationManager.cancel(NOTIFICATION_ID);
        //发送短信到手机上打开Muzei
        对于(节点节点:节点){
            Wearable.MessageApi.sendMessage(googleApiClient,node.getId(),
                    通知/打开,NULL).await();
        }
    }
    googleApiClient.disconnect();
}
 

然后在手持设备上侧的第3步是由<一个处理href="https://github.com/romannurik/muzei/blob/master/main/src/main/java/com/google/android/apps/muzei/MuzeiWearableListenerService.java">MuzeiWearableListenerService:

 公共无效onMessageReceived(MessageEvent的MessageEvent事件){
    字符串路径= messageEvent.getPath();
    如果(path.equals(通知/开)){

        //在这种情况下,我们启动发射意图用于该应用程序
        //但它可以是任何东西
        PackageManager packageManager = getPackageManager();
        意图mainIntent = packageManager.getLaunchIntentForPackage(
            getPackageName());
        startActivity(mainIntent);
    }
}
 

I want to start an activity in the handheld app and send some data when the user clicks on notification action button in the wear. I am using below code to build the notification in the wear.

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

Is it possible to start the activity in handheld through pending intent only or should we start an service/activity in the wear on clicking action button and from that activity/service send message to the device through message api. Any help would be appreciated!

解决方案

Your Wearable app is completely separate from the handheld app, with the only communication path being using the message/data APIs so unfortunately there is no way to directly trigger an action on the handheld from a notification generated on the Wearable.

You are correct in the approach that needs to be taken, however:

  1. Create a PendingIntent for your action that starts a service (an IntentService works perfectly)
  2. In that service:

    1. Connect to a GoogleApiClient
    2. If a connected device is found, start a ConfirmationActivity using an OPEN_ON_PHONE_ANIMATION (this displays a visible sign to your user that your app is opening something on their handheld)
    3. Send a message to the connected device using a set path (say, notification/open)

  3. In your handheld app, implement a WearableListenerService which will receive your message in the background:

    1. In the onMessageReceived() call, check the received message's path for the path you set (i.e., messageEvent.getPath().equals("notification/open"))
    2. If it matches, then start the appropriate activity on your handheld.

This approach is used in Muzei when starting the watch face having never activated the live wallpaper in the handheld app. Code covering each of these sections can be found on the Github repository.

Specifically, steps 1 and 2 can be found in 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();
}

And then the step 3 on the handheld side are handled by 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);
    }
}

这篇关于Android Wear:在点击穿通知操作按钮启动在手持设备的activty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆