从Android Wearable获取语音输入

时间:2014-09-16 18:53:14

标签: 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;
}

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

1 个答案:

答案 0 :(得分:0)

developer.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());
}

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