I have a Service which fetches some data from the web and updates a List which is "stored" in the Application.
Thus, i can access it from the main activity and use it for my ArrayAdapter.
When I update the data, the referenced item from the list is changed.
My Question is, how is the best practice to update the data in the Adapter in the main activity?
I have two solutions in mind, but I dont know if they are correct that way. Additional to that, I would like to implement a version which is not using much battery!
First: Thread which is called every second, updating the Adapter with notifyDataSetChanged():
private void startListUpdateThread()
{
Thread ListManageThread = new Thread() {
LinkItem listItem;
public void run() {
Log.d("DL", "List Update - start");
while(true)
{
runOnUiThread(UpdateGUIList);
try {
Thread.sleep(1000); //5Sekunden!
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("DL", "ERROR: InterruptedException - " + e.getMessage());
}
}
}
};
ListManageThread.start();
}
private Runnable UpdateGUIList = new Runnable() {
#Override
public void run() {
mFileAdapter.notifyDataSetChanged();
}
};
Second: Using a delayed Handler post
private final Handler handler = new Handler();
private void startListUpdate()
{
handler.removeCallbacks(UpdateListUI);
handler.postDelayed(UpdateListUI, 1000); // 1 second
}
private Runnable UpdateListUI = new Runnable() {
public void run() {
//Remove Elements first
removeDeletedItemsFromList();
//Update Adapter
mFileAdapter.notifyDataSetChanged();
handler.postDelayed(this, 1500); // 1,5 seconds
}
};
So, whats the best way to do it? Perhaps there is also an other Way to do it, but of which I haven`t thought of before!
Instead of using the handler you will use the Async task. Though handler works here but it's JAVA concept and Async Task is Android.
And you can update the list view by using the : mFileAdapter.notifyDataSetChanged(); only it's correct.
Instead of using Handler and Thread you can use AlarmManager. Just start the Alarm once and it will update your list with the defined period of interval without any Thread or Handler. You had created a service and you can reference to that service using the AlarmManager.
AlarmManager mgr = (AlarmManager) arg0.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(arg0, TestService.class);
PendingIntent pi = PendingIntent.getService(arg0, 0, intent, 0);
mgr.setRepeating(AlarmManager.RTC_WAKEUP, 2000, 2000, pi);
And in the TestService you can have the code to update your List.
Related
I need to repeat a task every 1 hour in the background (I am sending some info to my server).
I tried to use a service with a post delay handler calling it self.
handler = new Handler();
runable = new Runnable() {
#Override
public void run() {
try{
//sending info to server....
}
catch (Exception e) {
// TODO: handle exception
}
finally{
//also call the same runnable
handler.postDelayed(this, 1000*60*60);
}
}
};
handler.postDelayed(runable, 1000*60*60);
This did not work, in small time interval of 1 minutes it worked fine, when i changed it to 5 minutes it worked for about 5 repetitions and then the timing got wrong and after an hour the service shut down.
i want to try to use a AlarmManager but in the documentation
it says "As of Android 4.4 (API Level 19), all repeating alarms are inexact"
does anybody know how inexact it is? is it seconds? ,minutes?
can i rely on this to work on time?
does anybody have any other suggestions for repeating tasks in a service?
Thanks
You can use this This Code is for repeated calling on oncreate method or anyother thing
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 {
onCreate();
} catch (Exception e) {
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 1000); //execute in every 1000 ms
}
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 have to run a bit of code in the background every one second, the code will call a webservice which searches a database and returns a value to the application. My question is which method would be the most effective to do this? I have read up on Timers, Threads, AsyncTask and Services and each seem to have their pros and cons. Please can someone tell me which would be the best to use considering execution time and battery life.
Thanks
Update:
I decided to use Aysnc task to run my code in the background while using a TimeTask to trigger the AsyncTask at regular intervals. This way the operation is destroyed when I leave that particular activity
You should use the service to do the background operation but in your case you want to run code in 1 sec here is the example of service using handler it call in every 1 sec.
public class YourService extends Service {
private static final String TAG = "Your Service";
private final Handler handler = new Handler(){
#Override
public void handleMessage(Message msg) {
}
};
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
// Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
}
#Override
public void onDestroy() {
super.onDestroy();
// Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
handler.removeCallbacks(sendUpdatesToUI);
}
private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
/// Any thing you want to do put the code here like web service procees it will run in ever 1 second
handler.postDelayed(this, 1000); // 1 seconds
}
};
#Override
public void onStart(Intent intent, int startid) {
handler.removeCallbacks(sendUpdatesToUI);
handler.postDelayed(sendUpdatesToUI, 1000);//1 second
Log.d(TAG, "onStart");
}
}
and service can't run every time android idle the service within 3 or 4 hr i suggested you to use the foreground service to use your process long running.
For operations like this I tend to use a Service component. for the task itself i use an AsyncTask which will wait a set time before it repeats itself (using a while loop).
You will have to create a new Thread so that the call don't lock up the device if the call takes longer than expected. The AsyncTask is an easy way to use multithreading, but it lacks the functionality of repeating tasks. I would say that you are best of either using a Timer or the newer ScheduledExecutorService.
If you chose to use the Timer you create a TimerTask that you can hand it. The ScheduledExecutorService takes a Runnable instead.
You might want to wrap the thread in a Service (The Service does not provide a new Thread), but this is not always necessary depending on your needs.
As suggested in comment, you can also use the Handler.postDelayed(). Although you still need to create a new thread and then call Looper.prepare() on it:
class LooperThread extends Thread {
public Handler mHandler;
public void run() {
Looper.prepare();
mHandler = new Handler() {
public void handleMessage(Message msg) {
// process incoming messages here
}
};
Looper.loop();
}
}
(Code from Looper docs)
Also; calls to a webservice every second seems way too frequent, especially if the user is on a slow connection or there are data that needs to be transferred, try to reduce the calls as much as possible.
I think it's not only one solution, so it's up to you. You can try start thread with this run method:
private final int spleeptime = 1000;
public boolean running;
#Override
public void run() {
while (running) {
try {
int waited = 0;
while ((waited < spleeptime)) {
sleep(100);
waited += 100;
}
} catch (InterruptedException e) {
} finally {
// your code here
}
}
}
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.
In My application I want to check data after every second, using service.
I have tried to use Timer but it doesn't allow me to use 'runOnUiThread' in service.
In Activity Timer works fine.
or
Is there any other way to trace database at every seconds?
Implement a Runnable, that way you can use the runOnUiThread(runnable) functionality.
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
//do ui update
}
};
Edit: try this: More efficient way of updating UI from Service than intents?
Edit2: http://developer.android.com/resources/articles/timed-ui-updates.html
Use thread u can trace the data base every time
try{
thread=new Thread()
{
#Override
public void run() {
while(set) {
thread_Run();
}
}
};
}
catch(Exception e)
{
Toast.makeText(this, "error", Toast.LENGTH_SHORT).show();
}