In iOs Timer implementation is quite simple , Just declare a NSTimer object , then schedule the timer with selector and regular time interval.iOs framework is basically work on the Objective-C which is a sub set of native C language.So in iOS Timer concept is quite simple.
But for Android it throughly based on the Java language. So It inherit the java Timer concept. As per java timer concept we have to declare the timer object and for schedule the timer we have to declare a java class java.util.TimerTask object.This is create a separate thread in which run method continuous data is retrieved and update the GUI.
Here is the implementation.
//Android UI element declaration
TextView T=(TextView)findViewById(R.id.txtView);
// Timer object creation and Staring;
final Timer timer = new Timer();
// Timer task creation which will be executed on timer schedule
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
if(counter<=5)
counter++;
//Android UI get Updated continouly
t.setText("The counter is " + counter);
} else {
// If condition full filled the timer will stop here
timer.cancel();
timer.purge();
}
}
};
// Timer will schedule with task and periodical time interval.
timer.schedule(timerTask, 5000, 5000);
This is the logical implementation of the Java timer, but this is give an error when it trying to update the Android UI component , because Android can’t give the permission to update the UI element from any other thread . If we need to update the UI element we have to do it through the UI main thread.here is another way to update the GUI element . It is Handler .The main use of this is to schedule messages and runnables to be executed as some point in the future; and to enqueue an action to be performed on a different thread than your own. which help us to update the GUI element.. here is the implementation.
//Handler object declaration
final Handler h = new Handler();
//Android UI element declaration
TextView T=(TextView)findViewById(R.id.txtView);
// Timer object creation and Staring;
final Timer timer = new Timer();
// Timer task creation which will be executed on timer schedule
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
h.post(new Runnable() {
public void run() {
// Do something
if(counter<=5)
counter++;
//Android UI get Updated continually
t.setText("The counter is " + counter);
} else {
// If condition full filled the timer will stop here
timer.cancel();
timer.purge();
}
}
});
}
};
// Timer will schedule with task and periodical time interval.
timer.schedule(timerTask, 5000, 5000);
This is all about android timer illustration.
We have very small Android development lab in AM Infosystems Pvt. Ltd. but it consist of developers who have years of experience in Android application development for Android device family. Our Android developers can design any type app for better user experience. So if you want to transfer your complex idea into application with better app experience, AM Infosystems Pvt Ltd is your ultimate destination. You can hire Android programmer from us in very low cost with industry based expertise. Our Android developer will be taking care of all ideas to bring that ideas in reality and push it to the Google Play Store, which is your ultimate goal.
