gpt4 book ai didi

android - 从 Android 可穿戴设备获取语音输入

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:25:34 25 4
gpt4 key购买 nike

目前,Google Hangouts 和 Facebook Messenger 等应用程序能够接受来自 Android 可穿戴设备的语音输入,将其转换为文本并向用户发送回复消息。我已按照 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 级别太低。目前使用 Samsung Galaxy S3,4.4.2 API 19。显然,我无法使用此方法,所以我的问题是,Hangouts 和 Facebook Messenger 等应用程序如何接受语音输入并将该输入输入到我的设备上?

最佳答案

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

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

关于android - 从 Android 可穿戴设备获取语音输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25876360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com