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

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.

Related

How do you pause code in android studio?

I want to be able to change the background color of a button when pressed, then change it back half a second later. I have tried many things, but for some reason, most don't work.
For example, Thread.sleep(500); gives me an error, SystemClock.sleep(500); pauses the UI, and for some reason I cannot make a handler using Handler mHandler = new Hander(); No solution that I have found has worked so far.
You should never pause or sleep the main / UI thread!
What you will want to do is add code that runs in the background to change your button color. There are many ways to do this:
Handler
AsyncTask
POTs (Plain Old Threads)
If you do it by trying to pause the UI, you are also pausing everything else on the screen which is not what you want.

Imageview doesn't update. I've tried to use `invalidate();` but that didn't work

I'm trying to change the imageview 3 times in a short period of time.
I want to set the image, have sheldon say "Bazinga", then change imageview directly after he the audio is completed. And then I want to do that a second time.
Note that the first image is already set before calling this function:
sound = MediaPlayer.create(this, R.raw.bazinga);
sound.start();
page.setImageResource(R.drawable.counttotwofirst);
while(sound.isPlaying()){}
sound.start();
page.setImageResource(R.drawable.counttotwosecond);
while(sound.isPlaying()){}
sound.release();
page.setClickable(true);
I've tried to use invalidate(); but that didn't work.
Is there anyway to force update or at least wait until the imageview has been updated?
How long time is there between updates if imageviews?
Is there some other view I should use instead?
You need to re-think the design a bit. I am guessing you are running all of that code on the main (UI) thread of your app. The problem is that although you are changing the image resource, you are then causing the UI thread to block (sitting in a while() loop), so the UI updates don't get performed until after your entire function returns. You probably need some more pieces to achieve the sound/animation effects you want. Off the top of my head, probably create a background thread for performing the sound loading/playback so that's not done on the UI thread. Also, instead of sitting in a while() loop until the sound finishes, it's better to have a callback notify you that playback has completed (take a look at MediaPlayer::setOnCompletionListener()).

How To Pause A SurfaceView Thread From An Android Activity

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.

how to associate a thread or background process to our activity

i have to display text of the playing audio (file in sdcard). After given audio progress time i have to change the text of my TextView.
For this i think i should make a thread which monitor this stuff and behave like following:
if((player.isPlaying) == 0)
{
get new text and set to textView
pick relavent audio and play
}
can we associate a thread or some function like that to our Activity that constantly monitors it
I am sorry i am new to android please help me with some example or at least idea!!!
First, there is no such thing as "associate a thread with Activity" in Android. If you want to manage thread in parallel with Activity lifecycle - you can do that. You'll need to start when you want it, and stop it in onDestroy() (to put it briefly).
Also, in your particular case you do not need to have separate thread that monitors player. You need to set different listeners for you player. Assuming you're using standard Android's MediaPlayer, you need to set: OnErrorListener and OnCompletionListener.
This will allow you to not waste resources and have precise reaction on player's state changes.

Android: raise AlertDialog from background thread

In my activity there's some stuff going on in a background thread, which gets started in Activity_1. The processing of the background thread takes a while and I want to notify the user when it's completed via an AlertDialog. However, the user might have changed to Activity_2 or Activity_3 in the meantime and I would like to pop up the AlertDialog always in the current Activity.
Any idea how to realize this?
I ended up doing something like this in my background thread. It works, but not sure if it is a "good" solution.
Looper.prepare();
mActivity.showDialogAlertDefault();
Looper.loop();
Looper.myLooper().quit();
This probably means the right way is to use a Service, instead of a plain background thread. Your background work will be more resilient to being paused or shut down, and you don't have to worry about making an AsyncTask that is constantly keeping track of a different Activity parent.

Categories

Resources