安卓:runOnUiThread并不总是选择正确的线程? [英] Android: runOnUiThread does not always choose the right thread?
问题描述
我有,保持阅读的话,用户的活动,并使用具有文字转语音onUtteranceCompleted完成code时显示的东西。
里面onUtteranceCompleted我有这个code延迟功能与第二:
Runnable任务=新的Runnable(){
公共无效的run(){
// UI上运行
runOnUiThread(新的Runnable(){
公共无效的run(){
readWord();
}
});
}
};
worker.schedule(任务,1,TimeUnit.SECONDS);
这看起来效果很好,但我认为这是造成问题。
当我转动我的手机屏幕(我猜这将启动一个新的活动)。
我听说在后台看了一些话。我想这是因为 runOnUiThread()
这使得该活动在后台继续。
我怎么能避免运行2活动?我想preFER如果我没有从做一些怪异的补丁停止旋转屏幕!
感谢您
编辑:
公共无效readWord(){
如果(this.readingOnPause){
返回;
} txtCurrentWord.setText(CURRENTITEM [1]); this.hashAudio.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,字);
this.tts.setLanguage(Locale.US);
this.tts.speak(this.currentItem [1],TextToSpeech.QUEUE_FLUSH,this.hashAudio);
}
EDIT2:
工人的实例:
私有静态最终ScheduledExecutorService的工人= Executors.newSingleThreadScheduledExecutor();
好了,所以这是一个修复我想出了,如果有人有更好的解决办法,我在听。
- 我添加
的android:configChanges =keyboardHidden |方向
在androidmanifest 的内部活动
2。
再当屏幕旋转是被调用函数:
@覆盖
公共无效onConfigurationChanged(配置NEWCONFIG)
{
super.onConfigurationChanged(NEWCONFIG);
的setContentView(R.layout.streaming); initializeUI(); //包含所有findViewByID等...
}
I've got an activity that keeps reading words to the user, and using onUtteranceCompleted with textTospeech to display something when the code is completed.
Inside onUtteranceCompleted I have this code to delay a function with a second:
Runnable task = new Runnable() {
public void run() {
//runs on ui
runOnUiThread(new Runnable() {
public void run() {
readWord();
}
});
}
};
worker.schedule(task, 1, TimeUnit.SECONDS);
This seems like it works well, but I think it is causing a problem.
When I rotate the screen of my phone (I guess this starts a new activity).
I hear some words being read in the background. I guess this is because of runOnUiThread()
which makes the activity continue in the background.
How could I avoid 2 activities running ? I would prefer if I don't have to stop the screen from rotating on doing some weird patch!
Thank you
EDIT:
public void readWord() {
if (this.readingOnPause) {
return;
}
txtCurrentWord.setText(currentItem[1]);
this.hashAudio.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"word");
this.tts.setLanguage(Locale.US);
this.tts.speak(this.currentItem[1], TextToSpeech.QUEUE_FLUSH,this.hashAudio);
}
EDIT2:
instantiation of worker:
private static final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor();
Ok so this is a fix I've come up with, if someone has a better solution, I'm listening.
- I've added
android:configChanges="keyboardHidden|orientation"
inside the activity in the androidmanifest
2.
and then a function that is called when the screen is rotate:
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
setContentView(R.layout.streaming);
initializeUI(); //contains all the findViewByID etc...
}
这篇关于安卓:runOnUiThread并不总是选择正确的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!