Android - 一段时间后杀戮意图
Android - Kill intent after some period of time
我是 Android 的新人,正在开发语音转文本应用程序。我正在使用 Google API。我想让用户只能说2秒。 2 秒后弹出 window 应该关闭。谁能给我一些提示?
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;
}
}
您可以在要启动计时器的地方添加此代码,并且在方法中 运行 您必须编写关闭弹出窗口的代码
new java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
// your code here
}
},
5000
);
此处为 5 秒(5000 毫秒),您可以将其更改为您需要的任何时间段(以毫秒为单位)。
在 UIThread 中尝试 Handler 这可以让您在弹出窗口 window 关闭时延迟..添加代码以在 运行():
中关闭弹出窗口
runOnUiThread(new Runnable() {
@Override
public void run() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//close the window pop-up here
}
}, 2000);
}
});
希望对您有所帮助
我是 Android 的新人,正在开发语音转文本应用程序。我正在使用 Google API。我想让用户只能说2秒。 2 秒后弹出 window 应该关闭。谁能给我一些提示?
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;
}
}
您可以在要启动计时器的地方添加此代码,并且在方法中 运行 您必须编写关闭弹出窗口的代码
new java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
// your code here
}
},
5000
);
此处为 5 秒(5000 毫秒),您可以将其更改为您需要的任何时间段(以毫秒为单位)。
在 UIThread 中尝试 Handler 这可以让您在弹出窗口 window 关闭时延迟..添加代码以在 运行():
中关闭弹出窗口runOnUiThread(new Runnable() {
@Override
public void run() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//close the window pop-up here
}
}, 2000);
}
});
希望对您有所帮助