英文:
How to pause the current loop without pausing current running thread in Android / Java?
问题
在我的安卓应用程序中,在我的片段中,我有一个while循环,类似于
startMs=0, endMs=30000;
while(i < 10){
new Mp4Composer(inputPath, destPath)
.trim(startMs, endMs)
.listener(new Mp4Composer.Listener() {
@Override
public void onCompleted(){
}
})
.start();
startMs = endMs;
endMs += 30000;
i++;
}
这里的new Mp4Composer是一个线程...这个任务在每次循环迭代时执行...而不会完成上一个任务(任务仍处于处理状态)...循环跳到下一个迭代。所以现有任务不会产生任何输出...并且由于循环的原因跳到了下一个任务。
因此,我希望while循环在完成每个任务的new Mp4Composer之后等待。通过使用**public void onCompleted()**方法,我们能够确定何时完成每个任务的异步任务。
并且我不应该暂停当前运行的线程(在其中运行while循环的类线程)。原因是当我暂停放置循环的类线程时,整个UI和我的安卓应用程序都会被暂停。我对线程没有太多的了解。
英文:
In my android application, In my fragment, I have one while loop like
startMs=0, endMs=30000;
while(i<10){
new Mp4Composer(inputPath, destPath)
.trim(startMs, endMs)
.listener(new Mp4Composer.Listener() {
@Overide
public void onCompleted(){
}
}
.start();
startMs= endMs;
endMs+=30000;
i++
}
Here new Mp4Composer is a Thread... This task is executed for each iteration of the loop..without completing the previous task (on task still in processing state)... the loop jumped to the next iteration.so the existing task doesn't produce any output... and jumped to next task because of the loop.
So here what I want is while loop should wait to complete new Mp4Composer each task. By using public void onCompleted() method... we able to identify when that async task will finish for each task.
And here I should not pause the current running thread (where the class while loop running). The reason is when I pause while loop placed class thread, the total UI, and my android application gets paused. I haven't much knowledge about Thread.
答案1
得分: 1
使用处理程序而不是while循环。
英文:
use handler instead of while loop
答案2
得分: 1
Handler mHandler = new Handler();
int startMs = 0, endMs = 30000, i = 0;
Runnable action = new Runnable() {
@Override
public void run() {
new Mp4Composer(inputPath, destPath)
.trim(startMs, endMs)
.listener(new Mp4Composer.Listener() {
@Override
public void onCompleted() {
goNext();
}
})
.start();
}
};
void goNext() {
if (i < 10) {
startMs = endMs;
endMs += 30000;
i++;
mHandler.postDelayed(action, 2000); //2 second
}
}
英文:
Handler mHandler = new Handler();
int startMs=0, endMs=30000, i=0;
Runnable action = new Runnable(){
@Override
public void run() {
new Mp4Composer(inputPath, destPath)
.trim(startMs, endMs)
.listener(new Mp4Composer.Listener() {
@Overide
public void onCompleted(){
goNext();
}
}
.start();
}
};
void goNext(){
if(i < 10) {
startMs = endMs;
endMs += 30000;
i++;
mHandler.postDelayed(action, 2000); //2 second
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论