How To Pause A SurfaceView Thread From An Android Activity - android

I have followed this tutorial in great detail and have managed to understand and replicate the functions that I need from an Android SurfaceView:
http://obviam.net/index.php/moving-images-on-the-screen-with-androi/
I need to add a "pause" button in the game, which would be accessed from the activity. How would I go about pausing and then later continuing the thread from the activity?
I have tried adding a button to test it but from that point I can't fathom how to control the thread from the Activity. Any help will be very useful and much appreciated.
Thank you in advance

My opinion to you would be not to pause the thread when the virtual pause button in the game has been pressed, yet rather just alter your update method: eg
if(paused == true) {
// Don't do anything
}
else {
// Play
}
Yet to pause a your rendering thread, you can invoke the join() method on the thread.
I hope this helps.

I figured out a simple way to bypass the problem.
All I did was got the pause button to call an activity over the Surfaceview and hence it would pause the view. Then when I close that activity the thread continues from where it left off.

Related

Does the Back Button fail to call stop()?

My app currently has only one Form, which listens to the accelerometer sensor. In my start() method, I turn the listener on, and in the stop() method, I turn it off. I have verified that the listener turns off when I hit the Android's home button, but when I hit the back button, the application exits and the Android returns to the home screen, but the listener keeps going, which means the stop() method never got called. Is it my responsibility to handle the back button with code to call the stop() method? Or is this a bug in CodenameOne's framework? It seems to me that when the back button returns the user to the home screen, it should call stop() for me.
I am not sure about all the details of your issue, however you can resolve it by calling the setBackCommand on that one form.
yourForm.setBackCommand(
new Command("closing the sensor listener"){
#Override
public void actionPerformed(ActionEvent ev){
// your code to close the listener
}
}
);
I don't know about CodenameOne framework, but When app is visible and you press back button it calls all four methods in following order
1)onBackPressed()
2)onPause()
3)onStop()
4)onDestroy()
and when you press home button it calls only
1)onPause()
2)onStop()
methods
so, when you press back button onStop must be called.
You please put source code so that people can understand your problem clearly.
I don't know about CodenameOne framework, but I know the Android SDK.
Activity.onBackPressed() should be invoked when you use the back button. Just because your Activity is no longer visible does not mean it has been reaped, and this might explain why Activity.stop() is not invoked (immediately).
Depending upon your use case, Activity.onPause() might also work better.
HTH. Good luck w/your project.
This has been fixed. I've verified it, and it works correctly now.

Optimated back-button to camera-activity

in my android project i scan a image and then gets redirected to a new activity. In onPause i release my camera and in onResume i re-create it.
When using my back-button to go back to the camera-view; i get the feeling that it reacts slowly. I know for a fact that this i because the program dont change view before i created new camera instance in onResume.
My question is: How can i make the program show camera-activity view before making the camera instance to make everything "look" faster?
Hope i am clear! :)
refer to Android Camera online document:
Caution: On some devices, this method may take a long time to
complete. It is best to call this method from a worker thread
(possibly using AsyncTask) to avoid blocking the main application UI
thread.
Yes, you can open() camera in a background thread. I am not sure that this is what slows your activity display.

Thread UI sleep withtout freezing scrollview

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

thread runnable? or class? how to run a thread in background?

hey guys
I am trying to make a media player in which there is a seekbar.
Seekbar is running with a thread ; this thread is a runnable and not another class.
So far i have added the seekbar and now i want to stop the seekbar when pause button is pressed but there is nothing like :-
myThread.stop or myThread.pause??
Also i have to run one thread in background since if the user comes back to "now Playing"
activity he can see the seekbar at correct position
Thread is holding me back
Thanks
pause and stop and all those other methods don't exist any more (for very good reasons).
what you want to do is let your thread sit in a loop and do whatever it is you want it to do. Put in some settable flags that you can control from outside that will let the thread exit when you're done with it.
Keep in mind that threads aren't re-usable. Once you have run a thread you can't run it again.
Also, the seek bar can be set to the correct position when the user returns to your activity (I assume you'll keep the music playing in the background). There's no need to waste resources updating the seekbar's position while the activity is not in display.
What about using the buffering update callback provided by MediaPlayer?
It doesn't seem reasonable to me to have a thread running in the background to do this.

Pausing/Resuming A Game Thread Using wait() and notify() - Android

I have a main game thread but when the target score is achieved I have an activity that is launched called StageCleared which basically displays the stats to the user of their performance and then they can press a continue button to carry on with the game. This should switch focus back to the running thread that should continue execution, and thus display the game activity (with parameters i update after StageCleared has exectued).
It was suggested I use a package visible object that calls wait() on itself in the main game thread, and then notify() on itself from StageCleared in order to continue execution. My first problem is I can't seem to declare a package visible object that can be seen by all the classes in my package? Secondly, is this the best way to achieve what I'm intending to do or is there a better way?
Many thanks
To enable package visibile, leave the modifier blank:
static boolean mVarname = true;
mVarname is visible inside the package.
I work with a run flag to enable if the loop should do something or just "idle". I, too, dont know if this is a good way to do it :)
Use FutureTask and Executor, check it out in Java API.
It's like, you define the operation and FutureTask, and it'll wait until the task is completed.
Easier than wait()

Categories

Resources