英文:
How to force progressbar get visible just before a background thread starts its execution?
问题
I think this is a very simple question for many people in this community, however, I can't make this to work after several experiments; I would appreciate any help.
It is JAVA-android platform: The code needs to execute the next steps when the user clicks a button:
-
Make invisible the button (run in main thread)
-
Make visible a progress bar (run in main thread)
-
Download a file from internet (run in background thread)
-
Wait the download is completed
-
Make invisible the progress bar
-
Make visible again the button
That's it. It doesn't seem to be very difficult, however, it is not working as I need it.
This is the issue:
Step 3 gets executed before steps 1 and 2, ... I have tried several experiments with no success.
private void f1()
{
mDataBinding.btnPausePlay.setVisibility(btnVisibility);
mDataBinding.progressPausePlay.setVisibility(progressVisibility);
}
private void f2()
{
Thread xThread = new Thread( new Runnable()
{ @Override
public void run() // run in the background thread
{ httpRequest_noBackgroundThread( urlStr, urlParams, fileStr, itf ); }
});
try
{
xThread.start();
xThread.join(); // wait for the thread to finish
}
catch( Exception e ){ e.printStackTrace(); }
}
private void f3()
{
f1();
f2();
// continues execution ...
}
#######################################################
Based on Shagun Verma's feedback; It was the fix for my specific issue. Thank you!
private void f3()
{
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.execute(() -> {
f1();
f2();
// continues execution ...
});
}
(Note: The code sections are not translated.)
英文:
I think this is a very simple question for many people in this community, however, I can't make this to work after several experiments; I would appreciate any help.
It is JAVA-android platform: The code needs to execute the next steps when the user clicks a button:
-
Make invisible the button (run in main thread)
-
Make visible a progress bar (run in main thread)
-
Download a file from internet (run in background thread)
-
Wait the download is completed
-
Make invisible the progress bar
-
Make visible again the button
That's it. It doesn't seem to be very difficult, however, it is not working as I need it.
This is the issue:
Step 3 get executed before steps 1 and 2, ... I have tried several experiments with not success.
private void f1()
{
mDataBinding.btnPausePlay.setVisibility(btnVisibility);
mDataBinding.progressPausePlay.setVisibility(progressVisibility);
}
private void f2()
{
Thread xThread = new Thread( new Runnable()
{ @Override
public void run() // run in background thread
{ httpRequest_noBackgroundThread( urlStr, urlParams, fileStr, itf ); }
});
try
{
xThread.start();
xThread.join(); // wait for the thread to finish
}
catch( Exception e ){ e.printStackTrace(); }
}
private void f3()
{
f1();
f2();
// continues execution ...
}
###########################################################
Based on Shagun Verma's feedback; It was the fix for my specific issue. Thank you!
private void f3()
{
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.execute(() ->
{
f1();
f2();
// continues execution ...
});
}
答案1
得分: 1
为了实现这个,你需要在Java中使用Executors
。下面的代码将为你完成任务-
btnPausePlay.setOnClickListener(view -> {
btnPausePlay.setVisibility(View.GONE);
progressPausePlay.setVisibility(View.VISIBLE);
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.execute(() -> {
// 在这里实现你的文件下载代码,即
// httpRequest_noBackgroundThread( urlStr, urlParams, fileStr, itf );
handler.post(() -> {
btnPausePlay.setVisibility(View.VISIBLE);
progressPausePlay.setVisibility(View.GONE);
});
});
});
当你按下btnPausePlay按钮时,它将变为不可见,进度条将变为可见。之后,一旦下载完成,将发生相反的情况。
英文:
To accomplish this, you have to use Executors
in Java. The below code will be done the job for you-
btnPausePlay.setOnClickListener(view -> {
btnPausePlay.setVisibility(View.GONE);
progressPausePlay.setVisibility(View.VISIBLE);
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.execute(() -> {
// Implement your file download code here i.e.
// httpRequest_noBackgroundThread( urlStr, urlParams, fileStr, itf );
handler.post(() -> {
btnPausePlay.setVisibility(View.VISIBLE);
progressPausePlay.setVisibility(View.GONE);
});
});
});
When you press the btnPausePlay button, then it will become invisible and the progress bar becomes visible. After that, once the download is completed reverse will happen.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论