简体   繁体   中英

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

I have the next code in my Android Studio project

@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. 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. 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

public static final String 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. 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.

Starting this intent with just startActivity(Intent) is not supported. You must either use startActivityForResult(Intent, int), or provide a PendingIntent, to receive recognition results. Doc source

So try dealing with startActivityForResult();

Secondly, you should add a permission to 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. So for your next Activity you should add this "skill" to Manifest . Source . If you don't want, just handle this Activity as usual with Intent.ACTION_VIEW .

One nice answer for you in addition. This will fit you I guess.

Try it out. Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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