Java – Android runs the functions in the class in a new thread

I have this Code:

MyClass tmp = new MyClass();
tmp.setParam1(1);
tmp.SetParam2("Test");
tmp.setParam3("Test");
...

Then I have

tmp.heavyCalc();

During this heavy calculation operation, I must update the progress bar in the UI and show the user that it is using the update progress bar and some text to be displayed Now it doesn't work because I don't use threads. The app gets stuck and hanged, and then suddenly returns. The progress bar is 100% and all the text suddenly appears

So I decided to let my function run as a new thread In the internal definition of my class, I added the implementation runnable, so

public class MyClass implements Runnable{

Then I call the heavycalc() function to the new run() function created for me:

@Override
public void Run()
{
heavyCalc();
}

Now I do this:

Thread thread = new Thread(tmp);
tmp.run();

It works, but there is still no change in the UI, the application gets stuck, and then suddenly the progress bar 100% and the application returns What did I miss?

Solution

I suggest you use asynctask Its purpose is exactly what you want to do It allows you to run background operations and then update the UI with the results of such operations

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>