简体   繁体   English

使用语音识别android时将数据从一个活动传递到另一个活动

[英]Passing data from one Activity to another Activity when using speech recognition android

I have the next code in my Android Studio project 我的Android Studio项目中有下一个代码

@Override
public void startActivity(Intent intent) {
    boolean bandera = Intent.ACTION_SEARCH.equals(intent.getAction()) || RecognizerIntent.ACTION_RECOGNIZE_SPEECH.equals(intent.getAction());
    if (bandera) {
        intent.putExtra("usuario", usuarioIS);
    }
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    super.startActivity(intent);
}

When I search, I use the putExtra method to send some info of a user from the actual activity to another activity. 搜索时,我使用putExtra方法将用户的一些信息从实际活动发送到另一个活动。 But when I use the speech recognition, the startActivity method throws me an exception in my phone and android studio doesn't give info about the exception. 但是当我使用语音识别时,startActivity方法会在手机中引发异常,而android studio不会提供有关异常的信息。 Does anyone know why the speech recognition does not work? 有谁知道为什么语音识别不起作用?

This is my Manifest. 这是我的清单。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.ivan.saberespoder" >

    <!-- To access Google+ APIs: rex -->
    <uses-permission android:name="android.permission.INTERNET" />
    <!-- To retrieve OAuth 2.0 tokens or invalidate tokens to disconnect a user. This disconnect *** option is required to comply with the Google+ Sign-In developer policies -->
    <uses-permission android:name="android.permission.USE_CREDENTIALS" /> <!-- To retrieve the account name (email) as part of sign-in: -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <!-- To auto-complete the email text field in the login form with the user's emails -->
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />

    <activity
        android:name="com.facebook.FacebookActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/logopeq"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id" />

        <activity
            android:name=".PantallaPrincipal"
            android:label="iShots" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="android.app.default_searchable"
                android:value=".Busqueda" />
        </activity>
        <activity
            android:name=".Busqueda"
            android:label="@string/title_activity_busqueda" >
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>

            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>
        <activity
            android:name=".LoginActivity"
            android:label="@string/title_activity_login"
            android:windowSoftInputMode="adjustResize|stateHidden" >
        </activity>

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name=".Registro"
            android:label="@string/title_activity_menu_usuario" >
        </activity>
        <activity
            android:name=".MostrarShot"
            android:label="@string/title_activity_mostrar_shot" >
        </activity>
        <activity
            android:name=".Settings"
            android:label="@string/title_activity_settings" >
        </activity>
        <activity
            android:name=".AgregarShot"
            android:label="@string/title_activity_agregar_shot" >
        </activity>
        <activity
            android:name=".ProfileActivity"
            android:label="@string/title_activity_profile" >
        </activity>
        <activity
            android:name=".HelpActivity"
            android:label="@string/title_activity_help" >
        </activity>
        <activity
            android:name=".AboutActivity"
            android:label="@string/title_activity_about" >
        </activity>
    </application>
</manifest>

I'm new using Android Studio 我是使用Android Studio的新手

public static final String ACTION_RECOGNIZE_SPEECH 公共静态最终字符串ACTION_RECOGNIZE_SPEECH

Added in API level 3 Starts an activity that will prompt the user for speech and send it through a speech recognizer. 在API级别3中添加。启动一个活动,该活动将提示用户进行语音交流并通过语音识别器发送语音。 The results will be returned via activity results (in onActivityResult(int, int, Intent), if you start the intent using startActivityForResult(Intent, int)), or forwarded via a PendingIntent if one is provided. 如果使用startActivityForResult(Intent,int)启动意图,则结果将通过活动结果返回(在onActivityResult(int,int,Intent)中),或者如果提供的话,则通过PendingIntent转发。

Starting this intent with just startActivity(Intent) is not supported. 不支持仅使用startActivity(Intent)启动此意图。 You must either use startActivityForResult(Intent, int), or provide a PendingIntent, to receive recognition results. 您必须使用startActivityForResult(Intent,int)或提供PendingIntent来接收识别结果。 Doc source 文件来源

So try dealing with startActivityForResult(); 因此,尝试处理startActivityForResult();

Secondly, you should add a permission to Manifest : 其次,您应该向Manifest添加权限:

<uses-permission android:name="android.permission.RECORD_AUDIO" />

Third, if you define Action RecognizerIntent.ACTION_RECOGNIZE_SPEECH , so the system will try to open Activity which can handle it. 第三,如果定义了Action RecognizerIntent.ACTION_RECOGNIZE_SPEECH ,那么系统将尝试打开可以处理它的Activity So for your next Activity you should add this "skill" to Manifest . 因此,对于您的下一个Activity您应该将此“技能”添加到Manifest Source . 来源 If you don't want, just handle this Activity as usual with Intent.ACTION_VIEW . 如果您不想要,只需照常使用Intent.ACTION_VIEW处理此Activity

One nice answer for you in addition. 另外,您还可以得到一个不错的答案 This will fit you I guess. 我猜这将适合您。

Try it out. 试试看。 Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM