Android Remove Handler postDelayed Dynamically - java

I want to set dynamically auto scroll speed to WebView. In onCreate calling autoScroll(25) and remove it, nextly calling autoScroll(300) but when the apk is running the auto scroll speed is 25 so earlier called 'mHandler.postDelayed' do not removing. How to fix the problem?
Handler mHandler;
Runnable runnable;
WebView wv;
protected void onCreate(Bundle savedInstanceState) {
...
autoScroll(25);
mHandler.removeCallbacks(runnable);
autoScroll(300);
}
public void autoScroll(final int speed){
if(mHandler == null) {
mHandler = new Handler();
}
wv.post(runnable = new Runnable() {
#Override
public void run() {
wv.scrollBy(0, 1);
mHandler.postDelayed(this, speed);
}
});
}

mHandler.removeCallbacks(runnable);
will only remove any pending posts of Runnable r that are in the message queue. It will not stop an already running thread. You have to explicitly stop the thread. One way to stop that thread is to use a boolean variable as a flag and run your code inside runnable based on the value of that flag. You can take some hints from https://stackoverflow.com/a/5844433/1320616

Related

android - cancel runnable that already start running

I have a long process in the background to do. So onCreate, I post a runnable in my handler from handlerThread but I have a button that allows users to cancel it. It's possible to stop a Runnable after it starts?
#Override
protected void onCreate(Bundle savedInstanceState) {
Handler h = new Handler( HandlerThread.getLooper() );
Runnable runnable = //class that implements runnable;
h.post( runnable );
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
h.removeCallbacks(runnable);
}
}
}
but it seems that doesn't cancel runnable that already running, Or should I use postDelayed() with a huge delay?
Inside of your runnable have a boolean flag for running.
Your button then can set this flag to true/false, to stop it running.
You can implement pause, resume, or start, stop, it all depends on your usecase.
i.e. something like
while(running) {
// Your repeated background code
}
or
if(running) {
// do some one shot code, i.e. the user can stop it if they press the button before the if, but not after.
}
You could also have multiple steps, allowing you to cancel mid way.
if(running) {
// do step 1 code
}
if(running) {
// do step 2 code
}
Use below code
handler.removeCallbacksAndMessages(null);

Problems in running handlers?

I am trying to call my takepicture() method after every 1 minute. So, I tried using the handler class and then tried calling my method within its run function. However, when I tried doing a step wise debugging, it never enters the run method at all. Can anyone please suggest me what I am doing wrong? I am trying to call it from my fragment.
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mFile = new File(getActivity().getExternalFilesDir(null), "pic.jpg");
final Handler handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
Log.d("HandlerThread","This is from the HandlerThread");
takePicture();
handler.postDelayed(this, 60000);
}
};
}
Try this:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mFile = new File(getActivity().getExternalFilesDir(null), "pic.jpg");
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
Log.d("HandlerThread","This is from the HandlerThread");
takePicture();
}
}, 60000);
}
Rather than defining the handler.postDelayed within run method.I have just changed the call within your main thread itself.
Kindly mark it as answer if it solves your problem.
You never call any method that would run the Runnable. You only specified its behavior inside the run() function.
In order to start the Runnable, call something like handler.postDelayed(r, 0);
Just an info: please note that your Handler is still tied to the main Thread. See this answer and this one if you want to run it on a separate thread.
You should make an initial call to start the handler functionality.
ie , handler.post(r);

Android Handler for repeated task - will it overlap ? Timer-task VS handler VS alarm-manager

I'm trying to build an Android app which will repeatedly run some process every 10 mins.
As I found out Handlers are more reliable than timers or scheduling. So I'm going to develop my app using the Handlers using the given below codes.
I'm little bit concerned that the below codes will create separate Handlers at each time I start the app and keep them running parallel, may be since I'm creating the Handler on onCreate.
So what is the best way to keep only a single Handler runs in background at a time?
private Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handler = new Handler(); // new handler
handler.postDelayed(runnable, 1000*60*10); // 10 mins int.
setContentView(R.layout.activity_pro__sms);
}
private Runnable runnable = new Runnable() {
#Override
public void run() {
/* my set of codes for repeated work */
foobar();
handler.postDelayed(this, 1000*60*10); // reschedule the handler
}
};
You can extend Application class and do your work in it.
public class App extends Application {
private Handler handler;
#Override
protected void onCreate() {
super.onCreate();
handler = new Handler(); // new handler
handler.postDelayed(runnable, 1000*60*10); // 10 mins int.
setContentView(R.layout.activity_pro__sms);
}
private Runnable runnable = new Runnable() {
#Override
public void run() {
/* my set of codes for repeated work */
foobar();
handler.postDelayed(this, 1000*60*10); // reschedule the handler
}
};
}
And declare your class in manifest:
<application android:name=".App">
Edited
But it will work only if your app is running, otherwise you can use AlarmManager.
I decided to answer my own question since I've found out how to do it right way. The Android way. First of all what I was trying to do and posted in the question is a wrong approach to my requirement. Now I'm posting this so someone else will not do it wrong way but the following way.
Android has few options for timing.
Timer-task -> runs while application is alive. best for short term timing. Resource usage is higher.
Handler -> runs while application is alive. But not suitable to used as a scheduler. (this is what I've asked and it's not the correct way to do that). Handlers are the best way to do something repeatedly till the app is killed.
Alarm-manager -> The best way to schedule something to happen in future even if the app is killed. (this is what I should apply for my app).
This is what I figured out. Correct me if I'm wrong.
first define an utility class
public abstract class HandlerPeriodRunnable implements Runnable {
private Handler periodHandler;
private int msPeriod;
public HandlerPeriodRunnable(Handler periodHandler, int msPeriod) {
this.periodHandler = periodHandler;
this.msPeriod = msPeriod;
}
#Override
public void run() {
periodRun();
if (msPeriod > 0) {
periodHandler.postDelayed(this, msPeriod);
}
}
abstract public void periodRun();
}
then use it
final Handler mUIHandler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mUIHandler.postDelayed(new HandlerPeriodRunnable(mUIHandler, 1000) {
#Override
public void periodRun() {
}
}, 2000);
}

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