What is the best thing to use? [duplicate] - android

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).

Related

How to change ImageButton background for a while

I have an ImageButton (btnOpen), and I want to change its background, but just for a second, for example.
I know the method to do this (I use btnOpen.setBackgroundResource(my_resource); ), but how can I do that without making my UI non-responsive?
What's the best and simplest way, do I have to use something like Handler or runOnUIThread?
Thanks for help.
One way to deal with this is to preload your ressource programmatically using a Handler.
I think this post can help you https://stackoverflow.com/a/12523109/665823
But usually if the UI is getting block by loading a single image ressource this means your ressource is too big. Try checking this out first.
Use a handler
new android.os.Handler().postDelayed(new Runnable() {
#Override
public void run() {
imageView.setBackgroundResource(R.drawable.something);
}
}, 1000);

Preferred way to run smooth custom animations on Android

I need to run a complex custom animation in an Android app. For this, I wrote a function that needs to be called repeatedly and uses the current time-stamp to calculate positions, colors, shadows, etc. of View elements on the screen.
There seem to be a whole bunch of different approaches I could use to have this function called:
Standard Java Multi-Threading with Activity.runOnUIThread
"Tail-recursive" View.post calls
Timers
AsyncTasks
God knows what else... :)
In my current implementation I'm just calling my animation-function from a separate thread via runOnUIThread. While it works, it doesn't seem like a good idea as it might flood the event queue with messages faster than they can be handled or are needed given the screen refresh...
I posted a similar question for iOS a couple hours back and #IanMacDonald had an amazing answer for me that allows my function to be called once before every screen refresh and it makes for awesomely smooth animations. Is there something similar that I can do in Android, i.e. have it call my function every time the screen is about to be refreshed?
If possible, I would like to use a method that is as backward-compatible as possible, preferably API 7 or below...
I did some more reading and it seems like the current preferred approach would be to use the Choreographer's postFrameCallback method. Unfortunately Choreographer was only added in API 16, which is quite a bit too restrictive for my use-case.
Here's a way to do it via "recursive" View.postDelayed calls (i.e. the Runnable will reissue the View.postDelayed for itself) with the delay calculated from Display.getRefreshRate:
import android.app.Activity;
import android.view.Display;
import android.view.View;
public abstract class DisplayRefreshTimer implements Runnable {
public DisplayRefreshTimer(final View view) {
Activity activity = (Activity) view.getContext();
Display display = activity.getWindowManager().getDefaultDisplay();
final int delay = (int) (1000 / display.getRefreshRate());
view.postDelayed(new Runnable() {
#Override
public void run() {
view.postDelayed(this, delay);
DisplayRefreshTimer.this.run();
}
}, delay);
}
}
To use this, just sub-class it, override run() and instantiate passing it your current view:
new DisplayRefreshTimer(currentView) {
#Override
public void run() {
// Do your magic here...
}
};
If you're targeting API 17+, you can use View.getDisplay instead of going through the activity, but then you might as well look into Choreographer instead as mentioned above.
Please don't let this preliminary answer stop you from posting other ideas. I wouldn't be surprised if there is a better solution still hiding out there somewhere. Maybe in the OpenGL framework? At first glance I couldn't find anything there but I'll keep looking, too.

Updating UI from TimerTask

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

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