Perform Functions Synchronously
Solution 1:
Try the following.
myThread= newThread(newRunnable(){
@Overridepublicsynchronizedvoidrun() {
// TODO Auto-generated method stub
Write your stuff..............
....................
}
}).start();
Solution 2:
Is there really need to implement such function synchronously? Android UI is asynchronous and for good reason. You did not write how did you implement anim, vibration and sound methods. I am not sure they can be implemented this way, especially animation. Drawing in android should be usually done in main thread. If you want to handle application pause, or key events, or cancel your ringing, you should be able to react on incoming events, and your current thread does not allow it.
I suggest you check API how to play a sound and how to vibrate. Then use any event to start action. Start vibrator, start sound playing and begin animation. Use handler to send delayed message after 3 seconds to stop it. Cancel sound play, vibrating, stop redrawing animation. And use handler after 15 seconds from stop to start it again. It seems important to me, where will be background animation and what widget will be used for it? You can do that using SurfaceHolder, but again, unless you NEED to use synchronous code, do not do that.
Solution 3:
Without seeing all the code I am not sure if any of it needs to be on the UI thread, but probably the animation does. My thought was to have each in its own thread, and just have a play, pause, and release methods. So after 3 seconds tell them all to pause. And then tell them all to play. The animation will have to post something to the UI thread from its run method though. One other problem ... depending on how closely synchronized you need them this may be problematic because there may be a significant delay in playing sounds on older android versions.
EDIT: Can you also update your question to just give a little more detail of what goes wrong as in, do they all start around the same time, play one after the other or something else.
Here is a stab at some of the code. If you add the below code to your class, then create and start the threads somewhere sensible (like the view creation)
Sounds=newSound();
s.start();
Vibratevi=newVibrate(v);
vi.start();
and set their release to true in your onStop() method. Finally, in run method of your handler instead of:
anim();
vibration();
sound();
have:
s.play = true;
vi.play = true;
anim();
// classes to add
classSoundextendsThread
{
publicbooleanplay=false;
SoundPool sp;
int buzzer;
publicbooleanreleased=false;
publicSound()
{
sp = newSoundPool(5, AudioManager.STREAM_NOTIFICATION, 0);
buzzer = sp.load(this, R.raw.buzzer, 0);
}
publicvoidrun()
{
while (!released)
{
if (play)
{
sp.play(buzzer, 1,1, 0, 0, 1); //start alert tone
play = false;
}
else
{
try
{
Thread.sleep(5);
}
catch (Exception ex) {}
}
}
// release the sound pool
}
}
classVibrateextendsThread
{
publicbooleanplay=false;
publicbooleanreleased=false;
intdash=1000;
intmedium_gap=500;
Vibrator v;
long[] pattern = { 0, // Start immediately
dash, medium_gap, dash , medium_gap };
publicVibrate(Vibrator v)
{
this.v = v;
}
publicvoidrun()
{
while (!released)
{
if (play)
{
// Only perform this pattern one time (-1 means "do not repeat")
v.vibrate(pattern, -1);
play = false;
}
else
{
try
{
Thread.sleep(5);
}
catch (Exception ex) {}
}
}
}
}
Post a Comment for "Perform Functions Synchronously"