How to use android timer - android-layout

I am developing an android app.In my main activity i have two layouts. 1st relative Layout which have visibility gone and after that one LinerLayout which is visible.In my activity class,I want to set the timer so that after 3 second my LinerLayout should be gone and RelativeLayout should be visible.How to do that ?

you can use java.util.Timer to achiever this.
For repeating task, use following:
new Timer().scheduleAtFixedRate(task, after, interval);
For a single run of a task, use following:
new Timer().schedule(task, after);
task: Method that you to be executed.
after: time interval for initial execution of timer.(in milliseconds)
interval: intermediate time to repeat alarm
for your reference:
class UpdateTimeTask extends TimerTask {
public void run() {
firstLinearLayout.setVisibility(View.VISIBLE);
secondRelativeLayout.setVisibility(View.VISIBLE);
}
}
To start timer:
timer = new Timer();
timer.schedule(new UpdateTimeTask(), 3000);

Related

How to use properly Timer with thread in Xamarin?

I would like to use a timer , to execute a time, pause and stop.
has spent several days that i search how to use in the best way a timer because, as you know, it does not exist a timer directly unless to create it.
so, I followed these informations by creating a timer, using a timespan, timercallback and stopwatch :
Timer doesn't contain in System.Threading at Xamarin.Forms
https://developer.xamarin.com/api/namespace/System.Timers/
https://developer.xamarin.com/api/type/System.Diagnostics.Stopwatch/
I think that stopwatch is the best. And with several manupulations, i have what i wanted, but the problem is that the time runs out that every time I press the run button, when I should press once on the button.
With a Thread too, I had done but it dis not change anything. I do not know why it do that.
And it is the same with a Datetime, I would like that the time of the Datetime continues to run while i did not press another button to stop it.
If someone would have an idea avout it, really thank you in advance.
As I said, i think i have what i would. But I need to press just one time the button to run the chronometer instead of press it everytime with an incrementation of time milliseconds by milliseconds.
So, here is my chronoTimer.cs for that :
class TimerExpand
{
public int counter = 0;
public System.Threading.Timer threadTimer;
}
public static void Temps()
{
TimerExpand t_expand = new TimerExpand();
TimerCallback timerDelegate = new TimerCallback(GetTime);
System.Threading.Timer timer = new System.Threading.Timer(timerDelegate, t_expand, 0, 1000);
t_expand.threadTimer = timer;
}
public static void GetTime(object etat)
{
TimerExpand t_expand = (TimerExpand) etat;
t_expand.counter++;
}
private DateTime actualDate;
public static Stopwatch sw;
And then on my buttonTimer_OnClicked method :
actualDate = DateTime.Now;
sw = new Stopwatch();
sw.Start();
labelChrono.Text = string.Format("{0:00}:{1:00}:{2:00}:{3:00}", sw.Elapsed.Hours, sw.Elapsed.Minutes, sw.Elapsed.Seconds,
sw.Elapsed.Milliseconds);
labelDate.Text = DateTime.Now.ToString();
Temps();
}

How to run a function after a specific time

I want to as if there is any way to execute a function after a specific time in windows phone 7.? For instance, see this code in android:
mRunnable=new Runnable()
{
#Override
public void run()
{
// some work done
}
now another function
public void otherfunction()
{
mHandler.postDelayed(mRunnable,15*1000);
}
Now the work done in upper code will be executed after 15 seconds of execution of otherfunction().
And I want to know is this possible in any way in windows phone 7 also.?
Thanx to all in advance..
Although you can use the Reactive Extensions if you want, there's really no need. You can do this with a Timer:
// at class scope
private System.Threading.Timer myTimer = null;
void SomeMethod()
{
// Creates a one-shot timer that will fire after 15 seconds.
// The last parameter (-1 milliseconds) means that the timer won't fire again.
// The Run method will be executed when the timer fires.
myTimer = new Timer(() =>
{
Run();
}, null, TimeSpan.FromSeconds(15), TimeSpan.FromMilliseconds(-1));
}
Note that the Run method is executed on a thread pool thread. If you need to modify the UI, you'll have to use the Dispatcher.
This method is preferred over creating a thread that does nothing but wait. A timer uses very few system resources. Only when the timer fires is a thread created. A sleeping thread, on the other hand, takes up considerably more system resources.
You can do that by using threads:
var thread = new Thread(() =>
{
Thread.Sleep(15 * 1000);
Run();
});
thread.Start();
This way, the Run method wil be executed 15 seconds later.
No need for creating threads. This can be done much more easier using Reactive Extensions (reference Microsoft.Phone.Reactive):
Observable.Timer(TimeSpan.FromSeconds(15)).Subscribe(_=>{
//code to be executed after two seconds
});
Beware that the code will not be executed on the UI thread so you may need to use the Dispatcher.

Timers and javafx

I am trying to write a code that will make things appear on the screen at predetermined but irregular intervals using javafx. I tried to use a timer (java.util, not javax.swing) but it turns out you can't change anything in the application if you are working from a separate thread.(Like a Timer) Can anyone tell me how I could get a Timer to interact with the application if they are both separate threads?
You don't need java.util.Timer or java.util.concurrent.ScheduledExecutorService to schedule future actions on the JavaFX application thread. You can use JavaFX Timeline as a timer:
new Timeline(new KeyFrame(
Duration.millis(2500),
ae -> doSomething()))
.play();
Alternatively, you can use a convenience method from ReactFX:
FxTimer.runLater(
Duration.ofMillis(2500),
() -> doSomething());
Note that you don't need to wrap the action in Platform.runLater, because it is already executed on the JavaFX application thread.
berry120 answer works with java.util.Timer too so you can do
Timer timer = new java.util.Timer();
timer.schedule(new TimerTask() {
public void run() {
Platform.runLater(new Runnable() {
public void run() {
label.update();
javafxcomponent.doSomething();
}
});
}
}, delay, period);
I used this and it works perfectly
If you touch any JavaFX component you must do so from the Platform thread (which is essentially the event dispatch thread for JavaFX.) You do this easily by calling Platform.runLater(). So, for instance, it's perfectly safe to do this:
new Thread() {
public void run() {
//Do some stuff in another thread
Platform.runLater(new Runnable() {
public void run() {
label.update();
javafxcomponent.doSomething();
}
});
}
}.start();

How to create a Timer in J2ME or LWUIT?

I want to change incrementally every 5 milliseconds the backgroundTransparency of a LWUIT Button when it has focus. So I thought about timer , but I do not find any timer class in either J2ME or LWUIT Javadoc. So how to create timer ?
Use the timertask class provided by J2ME, i creates a thread and you can repaint after the time is elapsed. It kind of works like the setTimeout in javascript.
Timer aTimer = new Timer();
TimerTask ttask = new TimerTask() {
public void run() {
checkInternet();
}
};
aTimer.schedule(ttask, 1000);
J2ME have timer class see this example

Silverlight - fire of delayed action -

When I click on a button in Silverlight I want to run a method 2 seconds later once only for each time I click the button .. in the meantime the rest of the app keeps working .. obviously Thread.Sleep stops the whole UI .. how do I do this?
Inside handler start a new thread that will wait 2 seconds and execute your method. I mean something like
public void button_click(...)
{
(new Thread( (new ThreadWorker).DoWork).Start();
}
public class ThreadWorker
{
public void DoWork() { Thread.Sleep(2); RunMyCustomMethod();}
}

Resources