gpt4 book ai didi

Android 11 语音搜索按钮不显示

转载 作者:行者123 更新时间:2023-12-04 23:53:07 24 4
gpt4 key购买 nike

Android 11 中,当 targetSdk 设置为 30 并启用语音搜索时,麦克风图标不会显示在 搜索 View 。但是,如果 targetSdk 设置为 29,它可以正常工作。它也可以在 Android 10 设备上使用 targetSdk 30
30还有什么需要做的吗?

menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:title="Search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="always" />
</menu>

Manifest.xml

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />

<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>

searchable.xml

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="Search"
android:imeOptions="actionSearch"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"/>

MainActivity

override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.search, menu)

val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
val searchView: SearchView = menu.findItem(R.id.action_search).actionView as SearchView
val searchableInfo = searchManager.getSearchableInfo(componentName)
Log.d(TAG, "onCreateOptionsMenu: ${searchableInfo.voiceSearchEnabled}, ${searchableInfo.voiceSearchLaunchRecognizer}")

searchView.setSearchableInfo(searchableInfo)
searchView.setIconifiedByDefault(false)
return super.onCreateOptionsMenu(menu)
}

最佳答案

Android 11 不允许检查所有已安装的应用程序,因此我们需要添加一个 queries block 以允许应用程序看到 语音搜索 应用程序。

manifest 标记下方的 AndroidManifest.xml 中添加此内容,以显示麦克风图标:

 <queries>
<intent>
<action android:name="android.speech.action.RECOGNIZE_SPEECH" />
</intent>
</queries>

Package visiblility in Android 11

注意:如果您收到构建错误(不是 lint 警告)说 queries element not allowed,请确保更新构建工具版本:

https://android-developers.googleblog.com/2020/07/preparing-your-build-for-package-visibility-in-android-11.html

图片取自 https://android-developers.googleblog.com/2020/07/preparing-your-build-for-package-visibility-in-android-11.html

关于Android 11 语音搜索按钮不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63912808/

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