onClick() setting incrementation and delay - java

I'm new to Android and I'm working on a tiny project with a set of strict parameters. One of them is to have a multifunction button that increments a timer every time it's clicked, and that starts only after I didn't increment said timer for 3 seconds.
I found three or four ways on how to set an alarm of sorts with Handler, CountDownTimer, Timer, or some other way, but I'm confused on how I can do what I'm looking for with just the onClick() method.
The function to wait for 3s(), I'm calling it after:
public void wait3s()
{
Thread thread = new Thread() {
#Override
public void run()
{
while (!isInterrupted()) {
try {
Thread.sleep(3000);
runOnUiThread(new Runnable() {
#Override
public void run() {
count++;
threeS.setText(String.valueOf(count));
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
thread.start();
}
My onClick() just calls these:
public void onClick(View view)
{
increment();
wait3s();
startStop();
}
As you can see, part of the issue is that I'm calling the wait3s() there at every click, and I need a way to control that Thread/Timer (whatever) without it creating a new one at every click. I'm being a little dumb now, but I have been on this for a while and I'm still coming out empty, since I never worked with this before.
Another option for the wait3s() function that I found would be like in this other StackOverflow thread.
Thank you
PS: Sorry for the title, I couldn't find a better way to describe it, if you have it, and have the power to change it, please do.

Handler.removeCallbacks will effectively cancel a runnable.
boolean timerStarted = false;
clockHandler = new Handler();
OnClick(){
if (!timerStarted){
incrementTimer();
clockHandler.removeCallbacks(null);
clockHandler.postDelayed(new Runnable() {
#Override
public void run() {
// maybe kick off another handler/runnable here to start your timer
timerStarted = true;
}
}
}, 3 * 60 * 1000);
} else {
startStop();
}
}

Here's how I solve it. Although the suggestions were closer, they weren't exactly spot on to what I was looking for, in terms of logic, they weren't exactly helpful, creating more issues. Perhaps it's due to my explanation of the problem, perhaps because I'm new to Android Studio and the explanations shared here with me. Pardon me if it looks like I'm just using my own answer for internet points, I just had to put a lot more to understand this by myself than what I actually got from the answers shared here.
public void wait3s()
{
t.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
startStop();
}
});
}
}, 3000);
}
The variable t is a Timer(), and I had to import the classes java.util.Timer, and java.util.TimerTask. I called this method inside my increment() method and under onClick() I just have the increment() method. Turned out pretty neat.

Related

How to add a timer/clock in android studio [duplicate]

I want to be able to call the following method after a specified delay.
In objective c there was something like:
[self performSelector:#selector(DoSomething) withObject:nil afterDelay:5];
Is there an equivalent of this method in android with java?
For example I need to be able to call a method after 5 seconds.
public void DoSomething()
{
//do something here
}
Kotlin
Handler(Looper.getMainLooper()).postDelayed({
//Do something after 100ms
}, 100)
Java
final Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 100ms
}
}, 100);
The class to import is android.os.handler.
I couldn't use any of the other answers in my case.
I used the native java Timer instead.
new Timer().schedule(new TimerTask() {
#Override
public void run() {
// this code will be executed after 2 seconds
}
}, 2000);
Note: This answer was given when the question didn't specify Android as the context. For an answer specific to the Android UI thread look here.
It looks like the Mac OS API lets the current thread continue, and schedules the task to run asynchronously. In the Java, the equivalent function is provided by the java.util.concurrent package. I'm not sure what limitations Android might impose.
private static final ScheduledExecutorService worker =
Executors.newSingleThreadScheduledExecutor();
void someMethod() {
⋮
Runnable task = new Runnable() {
public void run() {
/* Do something… */
}
};
worker.schedule(task, 5, TimeUnit.SECONDS);
⋮
}
For executing something in the UI Thread after 5 seconds:
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
#Override
public void run() {
//Do something here
}
}, 5000);
Kotlin & Java Many Ways
1. Using Handler
Handler().postDelayed({
TODO("Do something")
}, 2000)
2. Using TimerTask
Timer().schedule(object : TimerTask() {
override fun run() {
TODO("Do something")
}
}, 2000)
Or even shorter
Timer().schedule(timerTask {
TODO("Do something")
}, 2000)
Or shortest would be
Timer().schedule(2000) {
TODO("Do something")
}
3. Using Executors
Executors.newSingleThreadScheduledExecutor().schedule({
TODO("Do something")
}, 2, TimeUnit.SECONDS)
In Java
1. Using Handler
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//Do something
}
}, 2000);
2. Using Timer
new Timer().schedule(new TimerTask() {
#Override
public void run() {
// Do something
}
}, 2000);
3. Using ScheduledExecutorService
private static final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor();
Runnable runnable = new Runnable() {
public void run() {
// Do something
}
};
worker.schedule(runnable, 2, TimeUnit.SECONDS);
you can use Handler inside UIThread:
runOnUiThread(new Runnable() {
#Override
public void run() {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//add your code here
}
}, 1000);
}
});
Thanks for all the great answers, I found a solution that best suits my needs.
Handler myHandler = new DoSomething();
Message m = new Message();
m.obj = c;//passing a parameter here
myHandler.sendMessageDelayed(m, 1000);
class DoSomething extends Handler {
#Override
public void handleMessage(Message msg) {
MyObject o = (MyObject) msg.obj;
//do something here
}
}
See this demo:
import java.util.Timer;
import java.util.TimerTask;
class Test {
public static void main( String [] args ) {
int delay = 5000;// in ms
Timer timer = new Timer();
timer.schedule( new TimerTask(){
public void run() {
System.out.println("Wait, what..:");
}
}, delay);
System.out.println("Would it run?");
}
}
More Safety - With Kotlin Coroutine
Most of the answers use Handler but I give a different solution to delay in activity, fragment, view model with Android Lifecycle ext. This way will auto cancel when the lifecycle begins destroyed - avoid leaking the memory or crashed app
In Activity or Fragment:
lifecycleScope.launch {
delay(DELAY_MS)
doSomething()
}
In ViewModel:
viewModelScope.lanch {
delay(DELAY_MS)
doSomething()
}
In suspend function: (Kotlin Coroutine)
suspend fun doSomethingAfter(){
delay(DELAY_MS)
doSomething()
}
If you get an error with the lifecycleScope not found! - import this dependency to the app gradle file:
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.4.0"
If you have to use the Handler, but you are into another thread, you can use runonuithread to run the handler in UI thread. This will save you from Exceptions thrown asking to call Looper.Prepare()
runOnUiThread(new Runnable() {
#Override
public void run() {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 1 second
}
}, 1000);
}
});
Looks quite messy, but this is one of the way.
I prefer to use View.postDelayed() method, simple code below:
mView.postDelayed(new Runnable() {
#Override
public void run() {
// Do something after 1000 ms
}
}, 1000);
Here is my shortest solution:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 100ms
}
}, 100);
If you are using Android Studio 3.0 and above you can use lambda expressions. The method callMyMethod() is called after 2 seconds:
new Handler().postDelayed(() -> callMyMethod(), 2000);
In case you need to cancel the delayed runnable use this:
Handler handler = new Handler();
handler.postDelayed(() -> callMyMethod(), 2000);
// When you need to cancel all your posted runnables just use:
handler.removeCallbacksAndMessages(null);
final Handler handler = new Handler();
Timer t = new Timer();
t.schedule(new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
//DO SOME ACTIONS HERE , THIS ACTIONS WILL WILL EXECUTE AFTER 5 SECONDS...
}
});
}
}, 5000);
I suggest the Timer, it allows you to schedule a method to be called on a very specific interval. This will not block your UI, and keep your app resonsive while the method is being executed.
The other option, is the wait(); method, this will block the current thread for the specified length of time. This will cause your UI to stop responding if you do this on the UI thread.
So there are a few things to consider here as there are so many ways to skin this cat. Although answers have all already been given selected and chosen. I think it's important that this gets revisited with proper coding guidelines to avoid anyone going the wrong direction just because of "majority selected simple answer".
So first let's discuss the simple Post Delayed answer that is the winner selected answer overall in this thread.
A couple of things to consider. After the post delay, you can encounter memory leaks, dead objects, life cycles that have gone away, and more. So handling it properly is important as well. You can do this in a couple of ways.
For sake of modern development, I'll supply in KOTLIN
Here is a simple example of using the UI thread on a callback and confirming that your activity is still alive and well when you hit your callback.
Handler(Looper.getMainLooper()).postDelayed({
if(activity != null && activity?.isFinishing == false){
txtNewInfo.visibility = View.GONE
}
}, NEW_INFO_SHOW_TIMEOUT_MS)
However, this is still not perfect as there is no reason to hit your callback if the activity has gone away. so a better way would be to keep a reference to it and remove it's callbacks like this.
private fun showFacebookStylePlus1NewsFeedOnPushReceived(){
A35Log.v(TAG, "showFacebookStylePlus1NewsFeedOnPushReceived")
if(activity != null && activity?.isFinishing == false){
txtNewInfo.visibility = View.VISIBLE
mHandler.postDelayed({
if(activity != null && activity?.isFinishing == false){
txtNewInfo.visibility = View.GONE
}
}, NEW_INFO_SHOW_TIMEOUT_MS)
}
}
and of course handle cleanup on the onPause so it doesn't hit the callback.
override fun onPause() {
super.onPause()
mHandler.removeCallbacks(null)
}
Now that we have talked through the obvious, let's talk about a cleaner option with modern day coroutines and kotlin :). If you aren't using these yet, you are really missing out.
fun doActionAfterDelay()
launch(UI) {
delay(MS_TO_DELAY)
actionToTake()
}
}
or if you want to always do a UI launch on that method you can simply do:
fun doActionAfterDelay() = launch(UI){
delay(MS_TO_DELAY)
actionToTake()
}
Of course just like the PostDelayed you have to make sure you handle canceling so you can either do the activity checks after the delay call or you can cancel it in the onPause just like the other route.
var mDelayedJob: Job? = null
fun doActionAfterDelay()
mDelayedJob = launch(UI) {
try {
delay(MS_TO_DELAY)
actionToTake()
}catch(ex: JobCancellationException){
showFancyToast("Delayed Job canceled", true, FancyToast.ERROR, "Delayed Job canceled: ${ex.message}")
}
}
}
}
//handle cleanup
override fun onPause() {
super.onPause()
if(mDelayedJob != null && mDelayedJob!!.isActive) {
A35Log.v(mClassTag, "canceling delayed job")
mDelayedJob?.cancel() //this should throw CancelationException in coroutine, you can catch and handle appropriately
}
}
If you put the launch(UI) into the method signature the job can be assigned in the calling line of code.
so moral of the story is to be safe with your delayed actions, make sure you remove your callbacks, or cancel your jobs and of course confirm you have the right life cycle to touch items on your delay callback complete. The Coroutines also offers cancelable actions.
Also worth noting that you should typically handle the various exceptions that can come with coroutines. For example, a cancelation, an exception, a timeout, whatever you decide to use. Here is a more advanced example if you decide to really start utilizing coroutines.
mLoadJob = launch(UI){
try {
//Applies timeout
withTimeout(4000) {
//Moves to background thread
withContext(DefaultDispatcher) {
mDeviceModelList.addArrayList(SSDBHelper.getAllDevices())
}
}
//Continues after async with context above
showFancyToast("Loading complete", true, FancyToast.SUCCESS)
}catch(ex: JobCancellationException){
showFancyToast("Save canceled", true, FancyToast.ERROR, "Save canceled: ${ex.message}")
}catch (ex: TimeoutCancellationException) {
showFancyToast("Timed out saving, please try again or press back", true, FancyToast.ERROR, "Timed out saving to database: ${ex.message}")
}catch(ex: Exception){
showFancyToast("Error saving to database, please try again or press back", true, FancyToast.ERROR, "Error saving to database: ${ex.message}")
}
}
For a Simple line Handle Post delay, you can do as following :
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// Do someting
}
}, 3000);
I hope this helps
You can use this for Simplest Solution:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//Write your code here
}
}, 5000); //Timer is in ms here.
Else, Below can be another clean useful solution:
new Handler().postDelayed(() ->
{/*Do something here*/},
5000); //time in ms
You can make it much cleaner by using the newly introduced lambda expressions:
new Handler().postDelayed(() -> {/*your code here*/}, time);
Using Kotlin, we can achieve by doing the following
Handler().postDelayed({
// do something after 1000ms
}, 1000)
If you use RxAndroid then thread and error handling becomes much easier. Following code executes after a delay
Observable.timer(delay, TimeUnit.SECONDS)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(aLong -> {
// Execute code here
}, Throwable::printStackTrace);
I created simpler method to call this.
public static void CallWithDelay(long miliseconds, final Activity activity, final String methodName)
{
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
try {
Method method = activity.getClass().getMethod(methodName);
method.invoke(activity);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}, miliseconds);
}
To use it, just call : .CallWithDelay(5000, this, "DoSomething");
Below one works when you get,
java.lang.RuntimeException: Can't create handler inside thread that
has not called Looper.prepare()
final Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 100ms
}
}, 100);
It's very easy using the CountDownTimer.
For more details https://developer.android.com/reference/android/os/CountDownTimer.html
import android.os.CountDownTimer;
// calls onTick every second, finishes after 3 seconds
new CountDownTimer(3000, 1000) {
public void onTick(long millisUntilFinished) {
Log.d("log", millisUntilFinished / 1000);
}
public void onFinish() {
// called after count down is finished
}
}.start();
I like things cleaner:
Here is my implementation, inline code to use inside your method
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 100ms
}
}, 100);
everybody seems to forget to clean the Handler before posting a new runnable or message on it. Otherway they could potentially accumulate and cause bad behaviour.
handler.removeMessages(int what);
// Remove any pending posts of messages with code 'what' that are in the message queue.
handler.removeCallbacks(Runnable r)
// Remove any pending posts of Runnable r that are in the message queue.
Here is another tricky way: it won't throw exception when the runnable change UI elements.
public class SimpleDelayAnimation extends Animation implements Animation.AnimationListener {
Runnable callBack;
public SimpleDelayAnimation(Runnable runnable, int delayTimeMilli) {
setDuration(delayTimeMilli);
callBack = runnable;
setAnimationListener(this);
}
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
callBack.run();
}
#Override
public void onAnimationRepeat(Animation animation) {
}
}
You can call the animation like this:
view.startAnimation(new SimpleDelayAnimation(delayRunnable, 500));
Animation can attach to any view.
Here is the answer in Kotlin you lazy, lazy people:
Handler().postDelayed({
//doSomethingHere()
}, 1000)
Kotlin
runOnUiThread from a Fragment
Timer
example:
Timer().schedule(500) {
activity?.runOnUiThread {
// code
}
}
A suitable solution in android:
private static long SLEEP_TIME = 2 // for 2 second
.
.
MyLauncher launcher = new MyLauncher();
launcher.start();
.
.
private class MyLauncher extends Thread {
#Override
/**
* Sleep for 2 seconds as you can also change SLEEP_TIME 2 to any.
*/
public void run() {
try {
// Sleeping
Thread.sleep(SLEEP_TIME * 1000);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
//do something you want to do
//And your code will be executed after 2 second
}
}

Activity start too long

Hello I have a problem with opening Activity.
I'm calling startActivity() with Intent by clicking Button.
I need to wait 4-5 seconds before Activity shows up on the screen.
I know how to do.
itemimg = new ItemsInPacagesImageView(imglist1, this, nazovtripu, 0);
I have 17 times similar code (with other ImageViews) I have this in Method with name InitItemimg();
I tried put this method on OnStart activity with this thread
#Override
public void onStart() {
super.onStart();
timer = new Thread() { // new thread
public void run() {
Boolean b = true;
try {
sleep(20);
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
InitItemimg();;
}
});
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
}
}
};
timer.start();
}
But is no resolve my problem, please do you have some ideas? Thanks
excuse me, I figured so in this method (ItemsInPacagesImageView(imglist1, this, nazovtripu, 0);) on start id deserialization if is some deserialization in row is "fast" but if it's more in row (now 17) with deserialization program spend more time some seconds.
I resolve this problem with put explicit, class which i deserialization in method.
Now i deserialization once instead 17 times. and I safe more miliscond-seconds.

Android: trying to add a wait in between two animations, getting Object not locked by thread before wait() error

I'm new to Android and Java programming. I created an app for a Coursera class. It has two animations that occur repeatedly. I want them to animate like a drum beat - 1,2,1,2,1,2,1,2,1,2.
I wrote two recursive methods that run until the user clicks a Reset button. I tried adding a wait in between the method calls so the second would kick off right after the first one.
This is the part of my code that has the wait.
mHandler = new Handler(); // .os package class when importing
mLeftfoot = findViewById(R.id.leftfoot);
mRightfoot = findViewById(R.id.rightfoot);
mFootAnim = AnimationUtils.loadAnimation(this, R.anim.foot); //this looks to the foot.xml file for the animations
leftstepRecursive();
try {
wait(600);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
rightstepRecursive();
This part of my code shows the recursive methods, which could probably be written better I know. A lot of DRY...
private void leftstepRecursive() {
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
mLeftfoot.startAnimation(mFootAnim);
if (!pleaseStop)
leftstepRecursive();
}
}, mIntervalleft);}
private void rightstepRecursive() {
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
mRightfoot.startAnimation(mFootAnim);
if (!pleaseStop)
rightstepRecursive();
}
}, mIntervalright);
Any better ideas to implement this left, right, left, right, left, right, left, right, left, right, idea?
Or, what I can do to correct the wait?
I also tried this:
private void rightstepRecursive() {
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
Object testobj = mRightfoot.startAnimation(mFootAnim);
synchronized (testobj) {
testobj.wait(600);
}
if (!pleaseStop)
rightstepRecursive();
}
}, mIntervalright);
#LuigiMendoza. I tried thread(sleep). No errors, but both animations move simultaneously not sure why...
private void rightstepRecursive() {
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
mRightfoot.startAnimation(mFootAnim);
if (!pleaseStop)
rightstepRecursive();
}
}, mIntervalright);
try
{
Thread.sleep(500);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
I also get this message repeating in Logcat.
02-11 12:15:32.261: I/Choreographer(30480): Skipped 302 frames! The application may be doing too much work on its main thread.
You should really utilize the Animations framework and some of the classes it provides such as AnimationListeners and AnimatorSets. The Animations framework provides a lot of functionality that would really simplify what you are trying to do i.e. repeating animations, adding delays, timing animations, etc.
Here's a really bare bones example to give the basic idea:
AnimatorSet animations;
//Play the animation
private void playAnimation(){
//If they haven't stopped the animation, loop it.
ObjectAnimator anim1 = ObjectAnimator.ofFloat(target, propertyName, values);
ObjectAnimator anim2 = ObjectAnimator.ofFloat(target, propertyName, values);
animations = new AnimatorSet();
animations.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
playAnimation();
}
});
animations.play(anim1).before(anim2);
animations.start();
}
//Called when cancel is selected
private void stopAnimation(){
animations.end();
}

Java Timer equivalent in Android

I recently began working with Java and am exploring Android development. I was trying to port over one of the Java programs I made, but I am having some difficulty with getting the java Timer to function the same way in Android. I read through a number of posts and they, for the most part, indicated that it would be better to use the Handler class in android as opposed to Timer.
This was my timer in Java:
playTimer = new Timer(1000/model.getFPS(), new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// do something
...
if( finished everything ) playTimer.stop();
}
});
And once a certain button was clicked, I would simply run "playTimer.start()" to start it.
As you can see, I had it set up so that the user could set the FPS they wanted (by simply setting the first parameter of the Timer class to 1000/model.getFPS()).
Now I've tried to do something similar in Android using handlers, but I am having some difficulty. It appears that the Handler ticks are not firing at the proper intervals. It seems that they are quite slow compared to what I need it to be. This is what I did in android so far:
public void startTimer() {
playHandler = new Handler();
startTime = System.currentTimeMillis();
playHandler.removeCallbacks(updateTimeTask);
playHandler.postDelayed(updateTimeTask, 0);
}
private Runnable updateTimeTask = new Runnable() {
public void run() {
// do something
...
if( finished everything ) playHander.cancel();
else {
playHandler.postDelayed(updateTimeTask, 1000/model.getFPS());
}
}
};
Excuse the semi-pseudocode. Can anyone shed any light? Thanks guys.
You can use a timer as below. The timer runs every second incrementing the counter. Displs the counter value in textview.
Timer runs on a different thread. SO you should set the text on the UI Thread.
The counter runs from 0 to 99. After 99 the timer is cancelled. Also cancel the timer when not required like in onPause().
public class MainActivity extends Activity {
TextView _tv,tv2;
Timer _t;
int _count=0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_tv = (TextView) findViewById( R.id.textView1 );
_t = new Timer();
_tv.setText(R.string.app_name);
_t.scheduleAtFixedRate( new TimerTask() {
#Override
public void run() {
_count++;
runOnUiThread(new Runnable() //run on ui thread
{
public void run()
{
_tv.setText(""+_count);
if(_count==99)
{
_t.cancel();
}
}
});
}
}, 1000, 1000 ); //change this value of 1000 to whatever you need.
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
_t.cancel();
}
}
If you decide not to use Timer (for whatever reason) you can just write a separate Thread that sleeps for x milliseconds and then wakes up and calls whatever Runnable you want it to call. That's going to be pretty precise. I have it working at the 10 millisecond level and it works quite nicely.
Just remember that it HAS to call a Runnable because a separate Thread can't have direct effect on anything on the main display thread.
public boolean keepPlayingAnimation = true
Handler h = new Handler()
Runnable updateDisplay = new Runnable(){
public void run(){
//do something in my display;
}
}
new Thread(){
public void run(){
while(keepPlayingAnimation){
try{
sleep(10);
}catch(Exception e){
}
h.post(updateDisplay);
}
}
}.start();
Just don't forget to set keepPlayingAnimation to false when you're done with this cause otherwise it will sit there running in the background for ever (or just about).
Take a look at Android Timer
It already has everything you need i guess. From ticking every 1 second to finish handly and so on.
Here is an example how to setup an TimerTask: setup
Not sure if you need such but i just remembered that i made this.

Android: runOnUiThread does not always choose the right thread?

I've got an activity that keeps reading words to the user, and using onUtteranceCompleted with textTospeech to display something when the code is completed.
Inside onUtteranceCompleted I have this code to delay a function with a second:
Runnable task = new Runnable() {
public void run() {
//runs on ui
runOnUiThread(new Runnable() {
public void run() {
readWord();
}
});
}
};
worker.schedule(task, 1, TimeUnit.SECONDS);
This seems like it works well, but I think it is causing a problem.
When I rotate the screen of my phone (I guess this starts a new activity).
I hear some words being read in the background. I guess this is because of runOnUiThread() which makes the activity continue in the background.
How could I avoid 2 activities running ? I would prefer if I don't have to stop the screen from rotating on doing some weird patch!
Thank you
EDIT:
public void readWord() {
if (this.readingOnPause) {
return;
}
txtCurrentWord.setText(currentItem[1]);
this.hashAudio.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"word");
this.tts.setLanguage(Locale.US);
this.tts.speak(this.currentItem[1], TextToSpeech.QUEUE_FLUSH,this.hashAudio);
}
EDIT2:
instantiation of worker:
private static final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor();
I would use a Handler instead of runOnUiThread().
For one thing, you're using a Thread that starts another Thread - why?
Secondly, if you create a simple Handler, it should kill itself on the rotate config change. IE:
private Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
// do your background or UI stuff
}
};
Then later use a Thread to call the handler, which will kick off whatever process you want to run on the UI thread:
new Thread() {
#Override
public void run() {
long timestamp = System.currentTimeMillis();
// thread blocks for your 1 second delay
while (System.currentTimeMillis() - timestamp <= 1000) {
// loop
}
handler.sendEmptyMessage(0);
}
}.start();
Ok so this is a fix I've come up with, if someone has a better solution, I'm listening.
I've added android:configChanges="keyboardHidden|orientation" inside the activity in the androidmanifest
2.
and then a function that is called when the screen is rotate:
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
setContentView(R.layout.streaming);
initializeUI(); //contains all the findViewByID etc...
}

Categories

Resources