我是Android新手,正在处理语音应用程序。我使用的是Google API。我想让用户只能说2秒钟。 2秒后弹出窗口应该关闭。谁能给我一些提示?
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;
}
}
答案 0 :(得分:2)
您可以将此代码添加到要启动计时器的位置,在方法运行中,您必须编写用于关闭弹出窗口的代码
new java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
// your code here
}
},
5000
);
这是5秒(5000毫秒),您可以将其更改为您所需的任何时间段(以毫秒为单位)。
答案 1 :(得分:1)
在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);
}
});
希望有所帮助