How to hide toast message “Your audio will be sent to google to provide speech recognition service.” in Android?

半城伤御伤魂 提交于 2019-12-03 10:17:22

问题


I am using google speech recognizer for integrating voice services in Android but while pressing on mic button this annoying toast message is showing. Please suggest me a way to hide this toast message.

This is my java code

public class FormActivity extends AppCompatActivity {

    AppCompatEditText mFeedbackView; 
    ImageView mFeedbackVoiceView;
    private final int REQ_CODE_SPEECH_INPUT_FEEDBACK = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_form);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mVisitFeedbackView = findViewById(R.id.feedback);
        mFeedbackVoiceView = findViewById(R.id.feedback_voice);

        mFeedbackVoiceView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                promptSpeechInputFeedback();
            }
        });
    }
    private void promptSpeechInputFeedback() {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, getString(R.string.speech_prompt));
        try {
            startActivityForResult(intent, REQ_CODE_SPEECH_INPUT_FEEDBACK);
        } catch (ActivityNotFoundException a) {
            Toast.makeText(getApplicationContext(), getString(R.string.speech_not_supported), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case REQ_CODE_SPEECH_INPUT_FEEDBACK: {
                if (resultCode == RESULT_OK && null != data) {
                    ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
                    mFeedbackView.setText(result.get(0));
                }
                break;
            }  
        }
    }
}

This question is duplicate of How to hide toast“ Your audio will be sent to google to provide speech recognition service.” in Speech Recognizer? but there are no solution for this.

Any help will be appreciated.


回答1:


Based on android regulations you cannot hide system toast messages as you don't have the accesses to the system View,

only in jailbrake android where you have access to the terminal you can try to do that.




回答2:


You can make a custom speech recogniser which will give you more control over the UI. MainActivity opens a new DialogFragment which implements RecognitionListener .

public class MainActivity extends AppCompatActivity implements VoiceRecognizerInterface {
    Button button ;
    TextView textView;
    String string;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.button);
        textView = findViewById(R.id.textView);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fragmentManager = getSupportFragmentManager();
                if (fragmentManager != null && fragmentManager.findFragmentByTag("dialogVoiceRecognizer") == null && !isFinishing()) {
                    VoiceRecognizerDialogFragment languageDialogFragment = new VoiceRecognizerDialogFragment(MainActivity.this,MainActivity.this);
                    languageDialogFragment.show(fragmentManager, "dialogVoiceRecognizer");
                }
            }
        });
    }

    @Override
    public void spokenText(String spokenText) {
        textView.setText(spokenText);
    }
}

An interface to signal the main activity after stt.

public interface VoiceRecognizerInterface {
    void spokenText(String spokenText);
}

The custom SpeechToText Dialog.

public class VoiceRecognizerDialogFragment extends DialogFragment implements RecognitionListener{

    ImageView micImage;
    TextView stateTV;
    TextView displayTV;

    private SpeechRecognizer mSpeechRecognizer;
    private Intent mSpeechRecognizerIntent;
    private Context context;
    VoiceRecognizerInterface signal;

    @SuppressLint("ValidFragment")
    public VoiceRecognizerDialogFragment(Context context, VoiceRecognizerInterface signal) {
        this.context = context;
        this.signal = signal;
    }

    public VoiceRecognizerDialogFragment(){

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.voice_recognizer_custom_layout ,container, false );

        //Mic tap to listen again
        micImage = view.findViewById(R.id.micImageView);
        //Displays Listening.. when recognizer is listening
        stateTV = view.findViewById(R.id.stateTV);
        //Displays message if error
        displayTV = view.findViewById(R.id.displayTV);

        mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(context);
        mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
        //Customize language by passing language code
        mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,"en");
        //To receive partial results on the callback
        mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS,true);
        mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
                context.getPackageName());
        mSpeechRecognizer.setRecognitionListener(this);
        mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
        micImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startListening();
            }
        });
        return view;

    }

    public void startListening(){
        mSpeechRecognizer.setRecognitionListener(this);
        mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
        changeUIStateToListening();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mSpeechRecognizer != null) {
            mSpeechRecognizer.destroy();
        }
    }

    @Override
    public void onBeginningOfSpeech()
    {
    }

    @Override
    public void onBufferReceived(byte[] buffer)
    {

    }

    @Override
    public void onEndOfSpeech()
    {
    }

    @Override
    public void onError(int error)
    {
        if(error == 7){
           changeUIStateToRetry();
        }
    }

    @Override
    public void onEvent(int eventType, Bundle params)
    {

    }

    @Override
    public void onPartialResults(Bundle partialResults) {
    }

    @Override
    public void onReadyForSpeech(Bundle params)
    {

    }

    @Override
    public void onResults(Bundle results)
    {
        ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        if(matches == null){
            return;
        }
        int i =0;
        String first ="";
        for(String s : matches){
            if(i==0){
                first = s;
            }
            i++;
        }
        // sending text to MainActivity using Interface
        signal.spokenText(first);
        this.dismiss();
    }

    @Override
    public void onRmsChanged(float rmsdB)
    {
    }

    public void changeUIStateToListening(){
        displayTV.setText("Tell us what you need");
        stateTV.setText("Listening...");
    }

    public void changeUIStateToRetry(){
        displayTV.setText("Didn't catch that. Try\nSpeaking again");
        stateTV.setText("Tap on mic to try again");
    }
}


来源:https://stackoverflow.com/questions/50692620/how-to-hide-toast-message-your-audio-will-be-sent-to-google-to-provide-speech-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!