RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS 奥利奥

RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS in Oreo

在大多数 Android 设备中,RecognitionService 将由 Google 的本机 'Now/Assistant' 应用程序提供。

直到 Android Oreo,我才能够使用以下简单代码查询 Google 识别器支持的语言:

final Intent vrIntent = new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS);

// vrIntent.setPackage("com.google.android.googlequicksearchbox");

getContext().sendOrderedBroadcast(vrIntent, null, new BroadcastReceiver() {

    @Override
    public void onReceive(final Context context, final Intent intent) {

                // final Bundle bundle = intent.getExtras();
                final Bundle bundle = getResultExtras(true);

                if (bundle != null) {

                    if (bundle.containsKey(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES)) {
                        Log.i("TAG", "onReceive: EXTRA_SUPPORTED_LANGUAGES present");

                        final ArrayList<String> vrStringLocales = bundle.getStringArrayList(
                                RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES);

                        Log.i("TAG", "onReceive: EXTRA_SUPPORTED_LANGUAGES size: " + vrStringLocales.size());

                    } else {
                        Log.w("TAG", "onReceive: missing EXTRA_SUPPORTED_LANGUAGES");
                    }

                } else {
                    Log.w("TAG", "onReceive: Bundle null");
                }

}, null, 1234, null, null);

但是,自 8.0+ 起,额外的 RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES 不再包含在响应中。

在我尝试将此作为错误归档之前,我想首先看看其他人是否可以复制 - 但还要检查是否有有序广播 behavioural change in API 26 我不知何故忽略了,这可能是原因。

提前致谢。

所以,我无法复制,但进一步的评论,如果你不设置包名称

vrIntent.setPackage("com.google.android.googlequicksearchbox");

然后它失败了,否则对我来说一切正常。

这是基本的 activity 我曾经测试过它。

package it.versionestabile.stackover001;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.speech.RecognizerIntent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.util.ArrayList;

import static java.security.AccessController.getContext;

/**
 * 
 */
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Intent vrIntent = new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS);
        vrIntent.setPackage("com.google.android.googlequicksearchbox");

        PackageManager packageManager = getPackageManager();

        for (PackageInfo packageInfo: packageManager.getInstalledPackages(0)) {
            if (packageInfo.packageName.contains("com.google.android.googlequicksearchbox"))
                Log.d("AAA", packageInfo.packageName + ", "  + packageInfo.versionName);
        }

        this.sendOrderedBroadcast(vrIntent, null, new BroadcastReceiver() {

            @Override
            public void onReceive(final Context context, final Intent intent) {

                // final Bundle bundle = intent.getExtras();
                final Bundle bundle = getResultExtras(true);

                if (bundle != null) {

                    if (bundle.containsKey(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES)) {
                        Log.i("TAG", "onReceive: EXTRA_SUPPORTED_LANGUAGES present");

                        final ArrayList<String> vrStringLocales = bundle.getStringArrayList(
                                RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES);

                        Log.i("TAG", "onReceive: EXTRA_SUPPORTED_LANGUAGES size: " + vrStringLocales.size());

                    } else {
                        Log.w("TAG", "onReceive: missing EXTRA_SUPPORTED_LANGUAGES");
                    }

                } else {
                    Log.w("TAG", "onReceive: Bundle null");
                }
            }

            }, null, 1234, null, null);
    }
}

我已经在 Android Studio 2.3 和 3.0.1 以及 API 26 和 27 模拟器上进行了测试。

以上代码一切正常。

但是如果你注释掉这一行:

vrIntent.setPackage("com.google.android.googlequicksearchbox");

在奥利奥上它不起作用。

而且我仍然建议以如下方式使用包管理器检查 Google 是否存在:

PackageManager packageManager = getPackageManager();

for (PackageInfo packageInfo: packageManager.getInstalledPackages(0)) {
    if (packageInfo.packageName.contains("com.google.android.googlequicksearchbox"))
        Log.d("AAA", packageInfo.packageName + ", "  + packageInfo.versionName);
// TODO - set a boolean value to discriminate the precence of google now
}

为了决定您是否拥有正确的版本 Google 现在。

希望对您有所帮助!