14

在大多数 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不再包含在响应中。

在我尝试将此作为错误提交之前,我想首先看看其他人是否可以复制 - 但还要检查 API 26 中是否存在我以某种方式忽略的有序广播行为更改,这可能是造成这种情况的原因。

提前致谢。

4

1 回答 1

15

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

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

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

这是我用来测试它的基本活动。

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;

/**
 * https://stackoverflow.com/questions/48500077/recognizerintent-action-get-language-details-in-oreo
 */
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");

在奥利奥上它不起作用。

而且我仍然建议使用 Package Manager 以如下方式检查 Google Now 的存在:

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 Now。

希望能帮助到你!

于 2018-02-06T22:45:06.623 回答