5

Currently applications like Google Hangouts and Facebook Messenger are able to accept voice input from Android Wearables, translate them to text and send reply messages to users. I have followed the tutorial at https://developer.android.com/training/wearables/notifications/voice-input.html and when I call the method outlined there:

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

I receive an error with the line RemoteInput.getResultsFromIntent(intent) stating that my API level is too low. Currently using a Samsung Galaxy S3, 4.4.2 API 19. Clearly, this method is not accessible to me, so my question is, how are applications like Hangouts and Facebook Messenger accepting voice input and getting that input onto my device?

Android
  • 1,230
  • 12
  • 20

1 Answers1

0

developers.Android states that RemoteInput.getResultsFromIntent(intent); is a conveince so that we do not need to parse the ClipData, so I did some research and discorvered what exactly was needed to parse 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 for anyone that is interested in developing a wearable application using notifications and voice input replies prior to the release of Android-L.

Android
  • 1,230
  • 12
  • 20