从Android Wearable获取语音输入

And*_*oid 5 android voice-recognition wear-os

目前,Google Hangouts和Facebook Messenger等应用程序可以接受来自Android Wearables的语音输入,将其翻译为文本并向用户发送回复消息.我已经按照https://developer.android.com/training/wearables/notifications/voice-input.html上的教程进行操作,当我调用那里概述的方法时:

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

我收到RemoteInput.getResultsFromIntent(intent)行的错误,指出我的API级别太低.目前使用三星Galaxy S3,4.4.2 API 19.显然,这种方法对我来说无法访问,所以我的问题是,环聊和Facebook Messenger等应用程序如何接受语音输入并将输入信息输入到我的设备上?

And*_*oid 0

Developers.Android 声明 RemoteInput.getResultsFromIntent(intent); 很方便,这样我们就不需要解析 ClipData,所以我做了一些研究并发现了解析此 ClipData 到底需要什么,这就是我解决问题的方法:

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());
}
Run Code Online (Sandbox Code Playgroud)

对于任何有兴趣在 Android-L 发布之前使用通知和语音输入回复开发可穿戴应用程序的人来说,这个答案应该很有用。