gpt4 book ai didi

java - Oreo 中的 RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS

转载 作者:搜寻专家 更新时间:2023-10-30 21:28:04 25 4
gpt4 key购买 nike

在大多数 Android 设备中,RecognitionService将由 Google 的原生“Now/Assistant”应用程序提供。

在 Android Oreo 之前,我可以使用以下简单代码查询 Google Recognizer 支持的语言:

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;

/**
* 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。

希望对您有所帮助!

关于java - Oreo 中的 RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48500077/

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