I want to load data from server synchronously and show it in a view like ListView or TextView without any pulling request each time. I need the data will be loaded if there is any changes in server.
Right now I am using asynctask to pull data from server but to load new data or update I need to do pulling request using a reload button.
I tried it using a thread which will run after a certain time. But I know there might have some better method obviously.
How can I do the same thing without the manual pulling request?
For example gmail app loads new emails when there is network access without any pulling request from user. I need the same thing in my Android Application.
Thanks in advance.
public void callAsynchronousTask() {
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
PerformBackgroundTask performBackgroundTask = new PerformBackgroundTask();
// PerformBackgroundTask this class is the class that extends AsynchTask
performBackgroundTask.execute();
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 50000); //execute in every 50000 ms
}
The AsyncTask does run on a separate thread, but cannot be started from other threads than the UI thread. The handler is there for allowing that.
Related
I would like to periodically check for updates doing network call every 30 sec and update listview accordingly, only if my screen is in foreground (thus not from service). What I am doing for that is -
private void refreshPeriodically()
{
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
new callToMyAsyncTask().execute(context);
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 30*1000); //execute in every 30 sec
}
but, that hangs my list while scrolling.
What should I do for that?
You shouldn't use handler. When you call handler.post() it will run on UI thread so your list will freeze. Start a new callToMyAsyncTask in run method in TimerTask without using handler and you can call timer.cancel() when your activity is not in foreground.
Also I think you should use scheduleAtFixedRate instead of schedule.
However I think you may have some problems without service. When you cancel timer it will not cancel current callToMyAsyncTask and when it call onPostExecute activity is no longer available. But I don't know it will crash or not.
I am using a timeschedular in android to schedule an AsyncTask in my app.I was able to work it out well,now i want every time the duration after which the task runs to be changed i am using this code
ip=3000;
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
play_duration=durat.get(vn);
ip=Integer.valueOf(play_duration);
ip=ShowImages.ip*1000;
Showtime s1 = new Showtime();
s1.execute(what,f12,transi,play_duration);
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0,ip);
i am trying to change the value of ip variable every time in the thread but it do not change and i do not know why my thread always run after the initial value of ip that is 3000 miliseconds, can someone suggest a way to do it..thanks
to change component on the UI use the runOnUiThread,
see here, here and here to do that thing on the background you can use something like -
while(true)
{
runOnUiThread() {
do some stuf here;
}
Thread.Sleep(300); //<--- that should be nothing for the user and enough for the cpu
}
if you need more accurate answer post your code and ill guide you through
I want to set up a timer in an Android application that will call a function after every 15/30/45 and n minutes when user login. But also it will stop timer when user log off. and timer begin from start if user login again. I want that option(15/30/45/n miutes) to be saved in database so that I can update list after sync.
Is Timer a good approach or I need to use alarm services? Or is there any system services required?
Is it possible to change previous doc/file in local phone database storage to new doc that is receiving through web server? is there any system services required to do so?
Use following code to call your function every 15/30/45
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
#SuppressWarnings("unchecked")
public void run() {
try {
"Your function call "
}
catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, "Timer value");
I am using the JSON parser to parse some pages but I would like to recall the parsing function every 30 seconds. How can i do that ?
One of the method to call a method every 30 seconds is by using postDelay of Handler see below code.
Handler handler;
handler=new Handler();
handler.removeCallbacks(run);
handler.post(run);
Runnable run=new Runnable()
{
public void run()
{
parsing();
handler.postDelayed(run,30000);
}
};
Another approach is by using "AlarmManager"
That is a weird need, only parsing when necessary would probably be a lot better.
Anyway, you should have a look at Timers and background services but be sure of what you are doing : if you create a background service that make a network call twice every minute, if that call is costly, you could cost a lot of data and/or battery to your users which is not a good idea.
You can do it using a timer.
Timer myTimer = new Timer();
After that you can call use the schedule method to call your json parser method.
myTimer.schedule(new TimerTask()
{
public void run() {
timerMethod();
}
}, 0, 1000);
private void timerMethod()
{
this.runOnUiThread(doSomething);
}
private Runnable doSomething = new Runnable() {
public void run() {
// Your code for doing something
}
I am getting data from a url for the monitoring window which show the list of visitors visiting the site,along with their ip,session id,time to live to site,no. of visit and their status from that url.i have to show data in a listview and after each 5 seconds getting new data.how can i make the listview that set data getting from url and after each 5 seconds update the data getting from server(means each after 5 seconds add new data to list).
can anyone help me?
I am using the update method like following.
Handler handler = new Handler();
Runnable updater = new Runnable() {
public void run() {
/*
* Update the list
*/
getVisitorDetailFromServer();
try {
Log.i("UPDATE", "Handler called");
// searchAdapter = getFeed(URL);
handler.postDelayed(this, 3000);
} catch(Exception e) {
Log.e("UPDATE ERROR", e.getMessage());
}
}
};
updater.run();
i am getting data correctly now using this method but problem is that after some time app is being crashed java.lang.indexoutofboundsexception occurs.
just try notifydatasetchange on list view. Run a thread and in that just do adater.notifydatasetchange.
To use scheduled timer , you can use this code:
private Timer myTimer;
myTimer = new Timer();
myTimer.schedule(new TimerTask() {
#Override
public void run() {
TimerMethod();
}
}, 0, 10000);
}
}
private void TimerMethod()
{
//This method is called directly by the timer
//and runs in the same thread as the timer.
//We call the method that will work with the UI
//through the runOnUiThread method.
this.runOnUiThread(Timer_Tick);
}
private Runnable Timer_Tick = new Runnable() {
public void run() {
//This method runs in the same thread as the UI.
//Do something to the UI thread here
}
};
try method below:
1 put your data in array or database or anyelse, which related to listview's adapter
2 create a Handler and use sendMessageDelaed(Message,5000) or postDelayed(Runnable,5000) to compose a loop.
3 call adapter.notifyDatasetChange(); in Runnable or handleMessage
in all, what you should do is update the dataset and call notifydatasetchange every 5 seconds.(Also can use TimerTask or other method)
See, if the datastructure (arraylist or other) containig your data is changing, ie. you are doing "Your_ARRAY_LIST" = new ArrayList();every time you get new data from the server, "notifyDataSetChanged()" wont work, as what it does is that it notifies the adapter that the arraylist containg data has been updated and the adapter then tries to refresh the data from the same address.But in your case the address of the data structure would have been changed.So if you are doing this, you need to set the adapter again in the "update()" method of your timertask.