【问题标题】:how to get inputs from wearable devices如何从可穿戴设备获取输入
【发布时间】:2015-11-19 13:59:15
【问题描述】:

我正在使用 Xamarin 平台实现通知系统,该平台扩展到可穿戴设备以发送通知。我还想从佩戴通知中获取用户的输入,并且我已经对其进行了编程,以便用户可以选择文本或使用语音。我按照以下教程进行操作

http://developer.android.com/training/wearables/notifications/voice-input.html

我的代码是:

void SendWearNotification (string message, string from)
{

    var valuesForActivity = new Bundle();
    valuesForActivity.PutString ("message", message);

     String groupkey = "group_key_emails";

    var intent = new Intent (this, typeof(MyMainActivity));
    intent.PutExtras (valuesForActivity);

    intent.AddFlags (ActivityFlags.ClearTop);
    var pendingIntent = PendingIntent.GetActivity (this, 0, intent, PendingIntentFlags.OneShot);

    var builder = new NotificationCompat.Builder (this)
        .SetAutoCancel (true)
        .SetContentIntent (pendingIntent)
        .SetContentTitle (from)
        .SetSmallIcon (Resource.Drawable.Iconlogo)
        .SetContentText (message)  //message is the one recieved from the notification
        .SetTicker(from)
        .SetGroup (groupkey)    //creates groups
        .SetPriority((int)NotificationPriority.High);
        //

    //for viewing the message in second page

    var pagestyle= new NotificationCompat.BigTextStyle();
    pagestyle.SetBigContentTitle (from)
        .BigText (messagefromapp);    //message from app is the one rerieved from the wcf app

    //second page 
    var secondpagenotification = new NotificationCompat.Builder (this)
        .SetStyle (pagestyle)
        .Build ();


    //intent for voice input or text selection 
    var wear_intent = new Intent (Intent.ActionView);
    var wear_pending_intent = PendingIntent.GetActivity (this,0,wear_intent,0);



    // Create the reply action and add the remote input
    setRemoteInput ();

    var action = new NotificationCompat.Action.Builder (Resource.Drawable.ic_mes, 
                                           GetString (Resource.String.messages), wear_pending_intent)
        .AddRemoteInput (remoteinput)
        .Build ();

    //add it to the notification builder
    Notification notification = builder.Extend (new NotificationCompat.WearableExtender ()
        .AddPage (secondpagenotification).AddAction(action)).Build ();


    //create different notitfication id so that we can as list
    if(notification_id<9){
        notification_id += 1;
    }else{
        notification_id=0;
    }

    var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
    notificationManager.Notify (notification_id+2, notification);
}

此方法在 GCMListnerService 类中实现。

根据上述链接中的教程,我可以使用以下代码检索用户选择或发言的输入数据:

private void getResponse(Intent intent){
    Bundle remoteInput = RemoteInput.GetResultsFromIntent(intent);
    if (remoteInput != null) {
        Toast.MakeText(this, remoteInput.GetCharSequence(EXTRA_VOICE_REPLY), ToastLength.Short);
    }
    //return null;
}

我的问题是我什么时候调用这个方法,我怎么知道用户是否选择了从可穿戴设备发送的文本。如果有任何我可以使用的事件。

【问题讨论】:

  • 首先,您尝试在帖子中包含的链接不可见,因此请编辑并更正该链接。其次,请更清楚地说明您的问题,我不确定是否有足够的信息来查看您想要做什么,您做了什么以及问题是什么。
  • 感谢您的评论,我希望现在明白我的意思了吗?

标签: wear-os


【解决方案1】:

我得到了解决方案。获取远程输入的方法(在我的例子中是“getresponse”)应该从创建通知时使用的活动的“Oncreate”方法调用。在我的情况下,当我创建通知的意图时,我使用的活动是“MyMainActivity”,因为你可以在代码中看到它。所以这意味着该方法将被调用两次,当应用程序运行时,以及当用户从穿戴设备中响应时。但只有在第二种情况下,“remoteinput.getResultfromIntent”才会有一个值。我希望它对有同样问题的人有所帮助。

【讨论】:

    猜你喜欢
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多