Updating UI from TimerTask - android

I am implementing an application that get the location data in certain intervals of time and checks the location got is the destination. If current location is the destination I want to Toast or update a TextField. I used TimerTask for this. But it is not giving me the correct output. While I searched I saw that TimerTask cannot handle changes in the UI. Is there any way to solve my problem?
I want to check in certain intervals and want to update UI

Just use runOnUiThread():
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(yourContext, "Data: ", Toast.LENGTH_SHORT).show();
}
});

you can use a handler to notify the ui thread.
http://developer.android.com/training/multiple-threads/communicate-ui.html
http://developer.android.com/reference/android/os/Handler.html

implement LocationListener instead of using TimerTask

Related

What is the best thing to use? [duplicate]

This question already has an answer here:
Threading in Android to process long running processes
(1 answer)
Closed 7 years ago.
guys. I want to make a runnable function with Postdelay about 50ms that will be adding Decimal number each time and send a result to activity so I can update this number in the View.I also need it keep running while switching to another activity. As far as I understand the best way is to put it in Sevice in a different thread so it doesn`t slow the UI. I am a beginner so I would like to get some advices from you guys. How should I do this?
Try to use a PostDelay handler for the delay.
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
#Override
public void run()
{
//DO something
}
}, 1000);
You can just use the View itself with View.postDelayed(yourRunnable, 50).

AsyncTask refreshing google map api v2

My android application retrieves some json data from remote API for each Marker (a Marker shows the position of a real device, there are less than 10 devices to watch) present on the map, and sets status of a device by changing a color of the marker according to some rule working on a given json data. I use AsyncTask to fetch json data and change a status of a device. I keep fetched data in ConcurrentHashMap<Device, Data>. So, I run a number of asynctasks, one for each device. I also use a custom info window (in fact custom InfoWindowAdapter) to show some more data about device. First I draw a markers and keep them in a map HashMap<Device, Marker>. I execute asynctasks one by one using:
new MyAsyncTask(markerMap).execute(device)
My custom InfoWindowAdapter overrides getInfoContents method, where some collected by asynctasks data are used to be shown in InfoWindow, when clicked.
Everything works fine. But now I want to refresh my markers every 10 sec. I have tried to do it using the following approach:
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask(){
#Override
public void run() {
new MyAsyncTask(markerMap).executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, device1);
new MyAsyncTask(markerMap).executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, device2);
}}, 0, 10000);
My question is if it is the right/ best way to refresh my map? Or should I use Runnable instead of AsyncTask, and within a Runnable send a message to Handler when fetching json data to update Marker? One more question: should I use Timer or ScheduledExecutorService? I have read some StackOverflow discussions but I dont know the final recommendation. Thanks.
Finally I have solved my problem. In mean time I have rewritten part of my program in order to use ScheduledThreadPoolExecutor with subclassed Runnable (instead of Asynctask). In addition I have added onResume() method where I restart ScheduledThreadPoolExecutor using: mScheduledThreadPoolExecutor.scheduleAtFixedRate(runnable, 0, interval, TimeUnit.MILLISECONDS);

Checking date every time in android

I am developing an app. The basic concept of the app is that I have to keep few video playing continuously. The list of videos i.e playist is maintained by webservice. The playlist has dates on which date which playlist is to be played. My problem is how to check the date and load playist accordingly? The videos from proper playlist should keep on playing. So do I need to keep a service in background which checks the date each day or what do i need to do?
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
public void run()
{
webservicecall();
}
}, 2000); // 2sec
this is an eg. to set time interval u can use it logically as u want
Use a specific function which gets called by itself after your required time and check the time and date condition if matches do the task..
if u wish a delay/ recursive call function can be provided..

Android pausing with thread but adding stuff to a text view

I have a linear layout in which I am placing a textview with the id "out". In my code i am getting this textview and calling out.append("Some string here");. What i want to do is have it use a thread.sleep(1000); to wait one second and then do another append. When i just use a for loop and iterate through it ten times it waits ten seconds and then updates the view at the end. How can i make this update the view in between the sleeps?
ps. The main reason for this is because i have another thread running with a bluetooth output stream and i want it to update the textview every time i send a byte to an arduino connected through a bluesmirf module. I can get it to send data but the updating of the screen happens at the end of the for loop. If i put a sleep in this loop it will wait the one second and then output to the arduino no problem. I just want to update it so i can see where things fail as they fail without using the logs.
Maybe you can use a postdelay handler
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
//Here is the code you want to run after 1 second
}
}, 1000);
The handler is not blocking, but maybe it can help
Excuse me for my bad english, good luck!

wait t time before launch an action?

I actually had a multiautocompletetextview, where i call host after 3 characters to have a dynamic search list.
But if the user put others characters, my code call host for each of them. So it must be very long.
Could I wait a moment (about 500 ms) before launching the action , in order to look if user do an action or not ? that's possible ?
You could use a separeted thread. When the user entered the text you could create a thread, make it sleep for 500ms and when it will wake up check if the text typed is changed.
EDIT
Create a Handler
private Handler h = new Handler();
Create a runnable that makes your dynamic search
private Runnable myrunnable = new Runnable() {
public void run() {
....
};
Then call your runnable in onTextChanged like
h.postDelayed(myrunnable, 500);
see Handler for more options/informations
Make sure that your threads will access the memory in a consistent way!

Categories

Resources