RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS in Oreo

前端 未结 1 766
花落未央
花落未央 2021-01-04 05:42

In most Android devices, the RecognitionService will be supplied by Google\'s native \'Now/Assistant\' application.

Up until Android Oreo, I was able to query the la

相关标签:
1条回答
  • 2021-01-04 06:21

    So, I could't replicate, but further to the comments, if you don't set the package name

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

    then it fails, otherwise all works fine to me.

    That's the basic activity I've used to test it.

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

    I've tested it both on Android Studio 2.3 and 3.0.1 and on emulator with API 26 and 27.

    All works fine with the above code.

    But if you comment out this line:

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

    on Oreo it doesn't work.

    And I still suggest to check the presence of Google Now with Package Manager in a way like this:

    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
    }
    

    In order to decide if you have the right version of Google Now.

    Hope it helps!

    0 讨论(0)
提交回复
热议问题