奥利奥的RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS

在大多数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中是否有我以某种方式忽略的有序广播行为更改,这可能是导致这种情况的原因。

提前致谢。


答案 1

因此,我无法复制,但如果您不设置包名称,则进一步删除注释

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");

在奥利奥上,它不起作用。

我仍然建议用包管理器检查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。

希望它有帮助!


答案 2

推荐