Reputation: 1023
I'm programming a thread that when 10 seconds have passed since beginning it execution it will make visible one component of the UI:
The code is the following:
Thread buttonThread=null; // this is a global variable
[...]
buttonThread = new Thread()
{
@Override
public void run()
{
try
{
super.run();
sleep(10000); //Delay of 10 seconds
} catch (Exception e)
{
}
finally
{
try
{
buttonThread.suspend();
cont.setVisibility(View.VISIBLE);
buttonThread.destroy();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
};
buttonThread.start();
But when I try to execute the buttonThread.suspend() I'm getting a java.lang.UnsupportedOperation exception.
I'm aware that using suspend is unsafe, and that's the reason it's deprecated, but I'd like to check first that suspending the thread does work and doing it by calling .suspend() looks the easiest way.
Could you, please, suggest me some possible solution so the thread shown in the code is suspended?
Upvotes: 0
Views: 66
Reputation: 2636
There is plenty of ways of doing something like this in android. The most common one being, using a Handler class like this.
add this import line.
import android.os.Handler;
and use this code to create a new runnable using handler class.
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// do your stuff here
}
}, DELAY_IN_MILLI_SECONDS);
Upvotes: 1
Reputation: 75629
I'm programming a thread that when 10 seconds have passed since beginning it execution it will make visible one component of the UI:
The thread is overkill here and its use its not justified. Use plain Runnable
and post it with required delay (postDelayed()
) instead - that would more than enough for your task.
Upvotes: 2