0

我是Android新手,正在致力于向文本应用发表演讲。我正在使用Google API。我想让用户只能说2秒。 2秒后弹出窗口应该关闭。任何人都可以给我一些提示吗?Android - 在一段时间后杀死意图

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; 
    } 
} 

回答

2

要启动计时器和方法来看,你必须编写代码以关闭弹出

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

这是5秒(5000毫秒),您可以添加以下代码您可以将其更改为毫秒所需的任何时间段。里面UIThread

-1

尝试处理程序这可以让你延缓当弹出窗口closes..add代码以关闭运行弹出():

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

希望它有助于

+0

你应该提供一些关于你的答案的信息或描述,以及你想用你的代码解释一下什么。 – Robert

+0

为什么投票表决? – ukama