0

I need to do some recursive tasks, say do something after every five seconds. How to achieve that? I will need to do the recursive task in the background, so I think I can go with a Started Service.

PS. What I am actually trying to do is: taking picture using camera after every five seconds, from a background service.

I found following code in the developer-guide. The method below resides in a custom class which extends Handler class:

@Override
      public void handleMessage(Message msg) {
      // Do Something
      // HOW CAN i MAKE A RECURSIVE CALL (TO SOMEHOW CALL THIS FUNCTION) AFTER 5 SECONDS?
      // Stop the service using the startId
      stopSelf(msg.arg1);
      }
  }

Can I call something like Thread.sleep(5000) just before stopSelf()? (Not making sense to me...)

Or can I call something like this.sendMessageDelayed(msgOb, 5000); ?

Thanks.

Anand
  • 1,315
  • 1
  • 12
  • 18
Shafiul
  • 2,832
  • 9
  • 37
  • 55
  • `this.sendMessageDelayed(msgOb, 5000);`. I don't see why you want to do this though... Why don't you just make a Thread that take a photo every 5 seconds? – m0skit0 Jul 20 '12 at 11:16
  • I need to use all these because that's what `started service` documentation asks to do, in the "Do Something" comment section – Shafiul Jul 20 '12 at 11:26
  • Ref: http://developer.android.com/guide/components/services.html#ExtendingService – Shafiul Jul 20 '12 at 11:27
  • stop... will you be able to take a photo without the user's intervention? – Aswin Kumar Jul 20 '12 at 11:35
  • 1
    I already can @AswinKumar, check this out: http://stackoverflow.com/questions/9744790/android-possible-to-camera-capture-without-a-preview – Shafiul Jul 20 '12 at 11:57
  • I don't understand what part of the documentation you're referring to... – m0skit0 Jul 20 '12 at 12:14
  • @m0skit0 http://developer.android.com/guide/components/services.html#ExtendingService – Shafiul Jul 20 '12 at 12:56
  • That doesn't say you have to send the message again every 5 seconds... Check my answer. – m0skit0 Jul 20 '12 at 14:32
  • @m0skit0 that's because I want to start over again after 5 seconds... I want to repeatedly do some work every 5 seconds. – Shafiul Jul 20 '12 at 17:46

2 Answers2

1

Ok then this is one way to do with a handler:

int counter=0;
private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MSG_TAKE_PIC:
                if(counter==5) {
                    mHandler.removeMessages(MSG_UPDATE_CALLTIME);
                    break;
                }
                else {
                    //take pic here and then
                    counter++;
                    sendEmptyMessageDelayed(MSG_TAKE_PIC, 5000);
                }

            }
        }
    };

and where you want to start taking the pic just:

mHandler.sendEmptyMessageDelayed(MSG_TAKE_PIC, 5000);
Aswin Kumar
  • 5,158
  • 5
  • 34
  • 39
  • Thanks for your post. What's the job of the counter? And shouldn't I need a call to `stopSelf()`? – Shafiul Jul 20 '12 at 12:57
  • Oh, I assumed you wanted to stop after 5 pics. Yes, if you are done processing, you can call stopSelf() to stop the service. – Aswin Kumar Jul 20 '12 at 16:26
0

I'm assuming you can modify the service that takes photos since you didn't say otherwise.

Why don't you just send a message for start to take photos and another for stop to take photos? The service will start taking photos every 5 seconds when you send the START message, until you send a STOP message.

Something like this:

class IncomingMessageHandler extends Hanlder implements Runnable {

    private Boolean takePhotos = false;

    public void handleMessage(Message msg) {
        switch(msg.what) {
        case START:
            takePhotos = true;
            new Thread(this).start();
            break;
        case STOP:
            synchronized (takePhotos) {
                takePhotos = false;
            }
            break;
        }
    }

    public void run() {
        while(takePhotos) {
            takePhoto();
            synchronized (takePhotos) {
                takePhotos.wait(5000);
            }
        }
    }
}

(this is the Service's hanlder implementation)

m0skit0
  • 25,268
  • 11
  • 79
  • 127