简体   繁体   中英

Sending Android speech recognition text to another activity

I'm currently try to use speech recognition in my android app. Essentially, I have a main activity, a loading activity, and a text display activity. When the button in main activity is pressed the microphone is turned on and then it transitions to the loading activity where it shows a gif with bouncing dots indicated that you're speaking into the microphone. After a certain amount of silence it sends the text to the test display activity. Right now I set it up where my main activity has a button that when pressed will send you to the loading screen. I'm assuming my code to activate speech recognition will go in main activity, but I'm unsure how to send the collected data to my text display activity. If anything is unclear please let me know. My code for my activities are given below.

Main activity

package app.test;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();

        ImageButton button = (ImageButton) findViewById(R.id.imageButton2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    openLoading();
            }
        });
    }

    public void openLoading(){
        Intent intent = new Intent(this, Loading.class);
        startActivity(intent);
    }


}

Loading activity

package app.test;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import java.util.Locale;

public class Loading extends AppCompatActivity {
private ImageView gifImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loading);
        getSupportActionBar().hide();

        gifImageView=findViewById(R.id.imageView2);

        Glide.with(Loading.this)
                .load(R.drawable.loading)
                .into(gifImageView);
    }

    @Override
    public void onBackPressed() {

    }
}

text display activity

package app.test;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

import org.w3c.dom.Text;

public class Tweet extends AppCompatActivity {
    private TextView txtResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tweet);
        txtResult = (TextView) findViewById(R.id.textView);
    }

}

Sharing data via Application class, or, you can save your data into a Bundle and call

Bundle b = new Bundle();
       b.putExtra("data", {your data collection}); 
intent.putExtra("b", b);
startActivity(intent);

thus you can retrieve you data back at here

protected void onCreate(Bundle b) {
    Intent dataIntent = getIntent();
    if (dataIntent != null) {
        Bundle b = dataIntent.getExtra("b");
        //...
    }
}

hope this could give some help

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