如何从可穿戴设备获取输入

how to get inputs from wearable devices

我正在使用 Xamarin 平台实现一个通知系统,它扩展到可穿戴设备以发送通知。我还想从磨损通知中获取用户的输入,并且我已将其编程为用户可以 select 文本或使用语音。我遵循了以下教程

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 中实现 class。

根据上述 link 中的教程,我可以使用以下代码检索用户 select 编辑或发言的输入数据:

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

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

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