我正在使用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;
}
我的问题是我何时调用此方法,如何知道用户是否选择了从可穿戴设备发送的文本。如果有任何我可以使用的事件。
答案 0 :(得分:0)
我得到了解决方案。获取远程输入的方法(在我的情况下为“getresponse”)应该从创建通知时使用的活动的“Oncreate”方法调用。在我的情况下,当我创建通知的意图时,我使用的actvity是“MyMainActivity”,因为你可以在代码中看到它。所以这意味着当应用程序运行时,以及当用户从磨损中退出时,将调用该方法两次。但在第二种情况下,“remoteinput.getResultfromIntent”将具有一个值。我希望这对有同样问题的人有所帮助。