My surfaceView is running in a thread because at times it's animated , however
most the time it just needs to be re-drawn once and left in response to some user interaction , but because its running in the thread onDraw is continually being called... the constant re-draws are slowing it down and making my phone warm :)
I would like to use invalidate(Rect) but can't see how to do this when the thread is controlling onDraw ...
Everything constantly redrawing can't be the way i should go , any help will be greatly appriciated ...
In your thread, you can call wait() after calling onDraw(). This will force the thread to sleep until prompted to continue working. Once the user has interacted and you want to resume animation, call notify() on the thread to wake it up and have it redraw.
If that doesn't exactly solve your problem, consider putting the wait() inside of an if statement so it will only happen after some condition is met.
Related
I'm developing a motion detection app, where I have a Thread that compares continously the screen frames for if there is movement, and if it is, it takes a capture and saves it.
Now I need to modify this, I need to do it just one time. After detecting movement, I need to start another method which will process some actions, but I no longer need the motionDetection thread, and due to some issues with it while I execute the new method, I would need to stop it and get out of it.
So, what would be the correct way to finish the thread when I start this new method?
Just return from the first thread, once you are done detecting the movement.
edit:
You should have a boolean variable inside a thread which will serve as a flag informing the thread if it should run or stop. If the flag is set to true the thread should run else the thread sholud stop and you should return from it.
If you want to finish the thread just set this variable to false.
I am developing an App that uses NFC.
When a new intent is discovered (NFC), the Activity goes to onStop state, then it executes onNewIntent code which executes a new Thread, in the middle of the Thread execution, the main Activity goes to onResume state, and then the thread keeps on with its execution.
Is there a way to control this sequence, I mean: Is there a way to execute onResume before the Thread, or at least to be sure that onResume is going to be executed before the Thread have finished?
EDIT:
To solve the issue, I put the Thread in sleep mode during 2000 milliseconds, this way the main activity takes the control and executes onResume, but this sounds like dirty workaround.
This way another problems came out, during those 2000 milliseconds, a new intent could be discovered, executing another Thread.
Thanks
When your thread completes, it could enqueue something (a Runnable, perhaps) to a queue. In onResume() you could then look in the queue and if there is anything there you could run it.
I figured out if I exit an activity while is running the AsyncTask in the middle, and when I try to start that activity again, I will have to wait until the previous AsyncTask to finish before the new AsynTask starts. I tried both thread.sleep and systemclock.sleep and it gives me the same result. Which make sense because I guess the thread that I closed and opened are the same. Its there a way to just cancel the AsyncTask if the user exit an activity? Because then the second time the user enter the same activity he wouldn't have to wait until previous finishes.
I tried asyntask.cancel(true) in onPause(), it doesn't work, same thing happened.
As I understand it, the only difference between Thread.sleep() and SystemClock.sleep() is that Thread.sleep() can be interrupted. That is, something like:
SystemClock.sleep(10*1000);
sleeps the calling thread for 10 seconds and that's that, you just have to wait. Whereas:
Thread.sleep(10*1000);
will also sleep the calling thread for 10 seconds. But if you have a reference to the sleeping thread from another thread, you now have the option of something like:
sleepingThread.interrupt();
which effectively wakes sleepingThread from its 10 second sleep.
In the context of your question, which is better for AsyncTask, I think it's still entirely up to your requirements. I don't know your exact code of course, but given your task's doInBackground is apparently doing some sleeping and given you want to be able to cancel your task at arbitrary times, Thread.sleep() might make more sense.
I have create a screen with two kind of views : normal view (buttons to receive up/down action ) and surfaceview. The SurfaceView implements SurfaceHolder.Callback and run on another thread and have a method name Tick().
When those buttons receive action, they will call method Tick(), and I want this method will run same thread with SurfaceView (for synchronize purpose), but don't know how to.
Please give me some idea for my issues.
Thanks :)
If you really want to run Tick() method in separate thread which also draws on the surface you can use HandlerThread for it. So you will be able to create Handler for it and post runnables which will be executed in this thread. But this also will put some restrictions on your drawing routine - you need to prevent it from sleeping or waiting because thread need to process message queue.
But actually I suppose any other reasonable way of synchronization will be easier than running this method on the same thread.
I'm trying to implements a game AI, and I got the following problem :
I'm calling a method from another class my UI Activity class, this method call itself some methods of the UI Activity class (to simulate click on screen among other things), and the things is, at the end of this method, I need to "pause" the game a few seconds to let the user see what did the AI.
So I tried running the method in another thread, but I got the error message providing from editing a widget from another thread. I tried to sleep the UI thread, but by doing that, the user can't use the scrollview anymore, and the changes aren't display before the sleep but after.
So I'd like to know how can I do this ?
(I've read some topics about AsyncTask, Handler, but can't make it work the way I need)
Thank's
You need runOnUiThread.
http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)
http://steve.odyfamily.com/?p=12