Receive a voice message from Android Wearable

Currently, apps like Google Hangouts and Facebook Messenger can receive voice messages from Android Wearables, translate them into text, and send back messages to users. I completed the tutorial at https://developer.android.com/training/wearables/notifications/voice-input.html and when I call the method described there:

private CharSequence getMessageText(Intent intent) { Bundle remoteInput = RemoteInput.getResultsFromIntent(intent); if (remoteInput != null) { return remoteInput.getCharSequence(EXTRA_VOICE_REPLY); } } return null; } 

I get an error with the RemoteInput.getResultsFromIntent line (intent), which indicates that my API level is too low. Currently using the Samsung Galaxy S3 API 4.4.2 API. Obviously this method is not available to me, so my question is how do apps like Hangouts and Facebook Messenger receive voice input and receive that input on my device?

+5
source share
1 answer

developers.ndroid reports that RemoteInput.getResultsFromIntent (intent); this means that we do not need to parse ClipData, so I did some research and realized what exactly needed to be analyzed with this ClipData, and this is how I solved my problem:

 private void getMessageText(Intent intent){ ClipData extra = intent.getClipData(); Log.d("TAG", "" + extra.getItemCount()); //Found that I only have 1 extra ClipData.Item item = extra.getItemAt(0); //Retreived that extra as a ClipData.Item //ClipData.Item can be one of the 3 below types, debugging revealed //The RemoteInput is of type Intent Log.d("TEXT", "" + item.getText()); Log.d("URI", "" + item.getUri()); Log.d("INTENT", "" + item.getIntent()); //I edited this step multiple times until I discovered that the //ClipData.Item intent contained extras, or rather 1 extra, which was another bundle //The key for that bundle was "android.remoteinput.resultsData" //and the key to get the voice input from wearable notification was EXTRA_VOICE_REPLY which //was set in my previous activity that generated the Notification. Bundle extras = item.getIntent().getExtras(); Bundle bundle = extras.getBundle("android.remoteinput.resultsData"); for (String key : bundle.keySet()) { Object value = bundle.get(key); Log.d("TAG", String.format("%s %s (%s)", key, value.toString(), value.getClass().getName())); } tvVoiceMessage.setText(bundle.get(EXTRA_VOICE_REPLY).toString()); } 

This answer should be useful to anyone interested in developing a wearable application using notifications and voice replies before the release of Android-L.

0
source

Source: https://habr.com/ru/post/1202707/


All Articles