Sometimes we need to display ads in our application.To display the multiple images one after another in some time interval write the following code:
- Create a customTimerTask class that extends the TimerTask class
public class customTimerTask extends TimerTask
{
Context mycontext;
int i =0;
public customTimerTask(Context c) {
mycontext=c;
}
@Override
public void run() {
advanceSearch.runOnUiThread(new Runnable() {
public void run() {
// Display the images
adImage.setTag(ServerUrl.BASE_SERVER_URL+ServerUrl.AD_UOLAOD_URL+imageArray.get(i).getImagePath());
imageLoader.DisplayImage(ServerUrl.BASE_SERVER_URL+ServerUrl.AD_UOLAOD_URL+imageArray.get(i).getImagePath(), advanceSearch, adImage);
i++;
if(i >= imageArray.size()){
i = 0;
}
}
});
}
}
2.
Then write the below line of code where you want to display images after getting the array of images
if(imageArray != null && imageArray.size() > 0)
{
Timer timer=new Timer();
/*
Schedule a task for repeated fixed-rate execution after a specific delay has passed.
Parameters
task the task to schedule.
delay amount of time in milliseconds before first execution.
period amount of time in milliseconds between subsequent executions.
*/
timer.scheduleAtFixedRate(new customTimerTask(advanceSearch),4000,4000);
}
Hope this will help you :)
0 Comment(s)