简体   繁体   English

Android-一段时间后终止意图

[英]Android - Kill intent after some period of time

I am new at Android and working on a speech to text app. 我是Android的新手,正在从事文本应用程序的演讲。 I am using Google API. 我正在使用Google API。 I want to allow users can only speak 2 seconds. 我想让用户只能说2秒钟。 After 2 seconds pop-up window should close. 2秒后,弹出窗口应关闭。 Can anyone give me some tips? 谁能给我一些提示?

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mic.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            promptSpeechInput();
        }
    });
}

public void promptSpeechInput()
{
    //This intent recognize the speech
    Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say Something");

    try {
        startActivityForResult(i, 100);
    }
    catch (ActivityNotFoundException a)
    {
        Toast.makeText(MainActivity.this,"Your device does not support",Toast.LENGTH_LONG).show();
    }
}

//For receiving speech input
public void onActivityResult(int request_code, int result_code, Intent i)
{
    super.onActivityResult(request_code, result_code, i);

    switch (request_code)
    {
        case 100: if(result_code == RESULT_OK && i != null)
        {
            ArrayList<String> result = i.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            resultTEXT.setText(result.get(0));
        }
            break;
    }
}

You can add this code where you want to start the timer and in method run you have to write the code for closing the pop up 您可以在要启动计时器的位置添加此代码,并且在方法运行时必须编写代码以关闭弹出窗口

new java.util.Timer().schedule( 
    new java.util.TimerTask() {
        @Override
        public void run() {
            // your code here
        }
    }, 
    5000 
);

Here it is 5 seconds (5000 milliseconds) you can change it to whatever time period you required in milliseconds. 这里是5秒(5000毫秒),您可以将其更改为所需的任何时间段(以毫秒为单位)。

Try Handler inside UIThread this lets you delay when the pop-up window closes..add the code to close the pop-up in run(): 在UIThread中尝试Handler,这可以让您延迟关闭弹出窗口。.添加代码以关闭run()中的弹出窗口:

runOnUiThread(new Runnable() {
            @Override
            public void run() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //close the window pop-up here
                    }
                }, 2000);
            }
        });

hope it helps 希望能帮助到你

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

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