Android 11语音搜索按钮不显示

问题描述

Android 11中,当targetSdk设置为30且启用了语音搜索时,麦克风图标未显示在SearchView上。但是,如果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应用程序。

voice search标签下面的AndroidManifest.xml中添加此图标,以显示麦克风图标:

manifest

Package visiblility in Android 11

注意:如果出现构建错误(不是棉绒警告),提示 <queries> <intent> <action android:name="android.speech.action.RECOGNIZE_SPEECH" /> </intent> </queries> ,请确保更新构建工具版本:

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...