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.
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);
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)
Related
First time ,i invoke HandlerThread.start() to handle the background service. after all the stuffs completed,I wanna to end this Thread by calling HandlerThread.quit().
Then the second time,I start this Handler,and checked the HandlerThread.isAlive(),the isAlive() return false, but when i invoke HandlerThread again by HandlerThread.start().
But I got the IllegalThreadStateException,why?
How can i really stop the HandlerThread before I invoke handlerThread.start() again safely?
onCreate(){
...............
CurrentLocationPresenter =
new CurrentLocationPresenter(getApplicationContext(),mHandler);
}
public void onClick(View v) {
int id = v.getId();
switch (id){
case R.id.showplacebutton:
showPlaceInMapActivity();
break;
case R.id.showgpsbutton:
if (mCurrentLocationPresenter.isAlive()){
break;
}
mCurrentLocationPresenter.start();
break;
private Handler mHandler = new Handler(){
#Override
public void handleMessage(Message msg) {
if (msg.what == CurrentLocationPresenter.WHATCODE){
mCurrentLatlng = (LatLng) msg.obj;
mTextView.setVisibility(View.VISIBLE);
if (mCurrentLatlng!=null) {
mTextView.setText(mCurrentLatlng.toString());
}
mCurrentLocationPresenter.getLooper().quit();
}
}
};
As stated, you can never call the start/run/execute method on a thread object more than once, as you will get the IllegalThreadStateException.
You can, however, use something such as an ExecutorService which will allow you to use the same Runnable multiple times.
Also, if you use a ThreadPoolExecutor, which is a descendant of ExecutorService, the memory and thread management is taken care of.
You can't call asyntask.execute() on same object more than one.
Always call MyasynTask asyntask=new MyasynTask();
asyntask.execute();
for more enter link description here
I'm not sure if this is the correct way to go about but I will try and explain what I want to do.
I have an Activity which creates a fragment called TemporaryFragment with a label. What I want to do is create and start a service with a Timer in it and that Timer then updates the time in that TextView.
The way I am thinking of going is somehow, when the Service is started, passing the TextView from the Activity to the Service and then the Service keeping a reference to it.
Another possible way is to make the Activity become a listener of the Service and then calling a method in the Service to update the TextView.
Any thoughts would be great and maybe some options.
Thanks in advance.
ADDITION
I'm sorry, I should also specify that I need this timer to run in the background. So when the application is sent to the background, I need the timer to carry on and only stop when I tell it to.
Service is not ideal for such minor task like this, moreover, Service can be run independently of activity. Also spawning new thread or using timer which introduces new thread into the application is not ideal for this relatively minor reason if you are thinking in the terms of mobile applications.
Instead use Handler in your fragment.
create handler in your fragment
private Handler mHandler = new Handler();
to execute your defined task call
mHandler.postDelayed(mUpdateTask, 1000);
or
mHandler.post(mUpdateTask);
and define your task in the fragment
private Runnable mUpdateTask = new Runnable() {
public void run() {
Toast.makeText(getActivity(), "hello world", Toast.LENGTH_SHORT).show();
mHandler.postDelayed(this, 1000);
}
};
If you are showing time-like information instead of countdown-like one, use
mHandler.removeCallbacks(mUpdateTimeTask);
in onPause() method to stop executing your task if the activity is not visible as updating UI isn't relevant and it saves battery (you start task again in onResume() method)
Basically, the idea behind the timer is eventually I am going to add some tracking into my application and therefore need it to continue running even if the application isn't in the foreground – Disco S2
Based on this comment I suggest you to use a local service which resides in the background, doing it's stuff (start a thread from Service#onStart), until it gets stopped by stopService(..).
Activities on the other hand may bind and unbind to that service (see: bindService(..)) to get notified about updates or to communicate with the service in any way.
I would use a more simple approach by using a Thread:
public class MainActivity extends Activity implements Callback {
private static final int MSG_UPDATE = 1;
private static final long INTERVAL = 1000; // in ms
private final Handler handler = new Handler(this);
private Thread worker;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case MSG_UPDATE:
updateView();
return true;
}
return false;
}
private void updateView() {
// TODO tbd
}
#Override
protected void onStart() {
super.onStart();
// start background thread
worker = new Thread(new Runnable() {
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
Thread.sleep(INTERVAL);
} catch (InterruptedException e) {
break;
}
// send message to activity thread
handler.sendEmptyMessage(MSG_UPDATE);
}
}
});
worker.start();
}
#Override
protected void onStop() {
super.onStop();
// stop background thread
worker.interrupt();
try {
worker.join();
} catch (InterruptedException e) {
}
worker = null;
}
}
You can use the TimerTask Class for this. Override the TimerTask.run() method and then add that TimerTask to Timer class.
Also check this question: controlling a task with timer and timertask
I have to run a bit of code in the background every one second, the code will call a webservice which searches a database and returns a value to the application. My question is which method would be the most effective to do this? I have read up on Timers, Threads, AsyncTask and Services and each seem to have their pros and cons. Please can someone tell me which would be the best to use considering execution time and battery life.
Thanks
Update:
I decided to use Aysnc task to run my code in the background while using a TimeTask to trigger the AsyncTask at regular intervals. This way the operation is destroyed when I leave that particular activity
You should use the service to do the background operation but in your case you want to run code in 1 sec here is the example of service using handler it call in every 1 sec.
public class YourService extends Service {
private static final String TAG = "Your Service";
private final Handler handler = new Handler(){
#Override
public void handleMessage(Message msg) {
}
};
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
// Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
}
#Override
public void onDestroy() {
super.onDestroy();
// Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
handler.removeCallbacks(sendUpdatesToUI);
}
private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
/// Any thing you want to do put the code here like web service procees it will run in ever 1 second
handler.postDelayed(this, 1000); // 1 seconds
}
};
#Override
public void onStart(Intent intent, int startid) {
handler.removeCallbacks(sendUpdatesToUI);
handler.postDelayed(sendUpdatesToUI, 1000);//1 second
Log.d(TAG, "onStart");
}
}
and service can't run every time android idle the service within 3 or 4 hr i suggested you to use the foreground service to use your process long running.
For operations like this I tend to use a Service component. for the task itself i use an AsyncTask which will wait a set time before it repeats itself (using a while loop).
You will have to create a new Thread so that the call don't lock up the device if the call takes longer than expected. The AsyncTask is an easy way to use multithreading, but it lacks the functionality of repeating tasks. I would say that you are best of either using a Timer or the newer ScheduledExecutorService.
If you chose to use the Timer you create a TimerTask that you can hand it. The ScheduledExecutorService takes a Runnable instead.
You might want to wrap the thread in a Service (The Service does not provide a new Thread), but this is not always necessary depending on your needs.
As suggested in comment, you can also use the Handler.postDelayed(). Although you still need to create a new thread and then call Looper.prepare() on it:
class LooperThread extends Thread {
public Handler mHandler;
public void run() {
Looper.prepare();
mHandler = new Handler() {
public void handleMessage(Message msg) {
// process incoming messages here
}
};
Looper.loop();
}
}
(Code from Looper docs)
Also; calls to a webservice every second seems way too frequent, especially if the user is on a slow connection or there are data that needs to be transferred, try to reduce the calls as much as possible.
I think it's not only one solution, so it's up to you. You can try start thread with this run method:
private final int spleeptime = 1000;
public boolean running;
#Override
public void run() {
while (running) {
try {
int waited = 0;
while ((waited < spleeptime)) {
sleep(100);
waited += 100;
}
} catch (InterruptedException e) {
} finally {
// your code here
}
}
}
I have a activity (myActivity) and a thread (MyThread) , both with a handler which allow me to send message between the UI thread and myCustomThread.
Now i would like to call periodically (10sec) the ALIVE message of the MyThread thread from MyActivity. How can i achieve that ?
MyActivity :
public void onResume()
{
super.onResume();
this.thread = new MyThread(activityHandler);
this.threadHandler = this.thread.gethandler();
threadMessage = this.threadHandler.obtainMessage();
threadMessage.what = AUTH;
this.threadHandler.sendMessage(threadMessage);
}
MyThread :
#Override
public void run()
{
Looper.prepare();
this.threadHandler = initHandler();
this.message = this.activityHandler.obtainMessage();
this.message.what = CONNECTED;
activityHandler.sendMessage(this.message);
Looper.loop();
}
private Handler initHandler()
{
return new Handler() {
public void handleMessage(Message msg)
{
switch(msg.what)
{
case AUTH :
{
auth();
break;
}
case ALIVE :
{
sendAlive();
break;
}
}
}
};
}
Thanks for your help
The correct solution really depends on what you are trying to put together...
This is a walk through for performing an action on a timer as well as how to use a delayed post (the preferred way of executing on a schedule because it doesn't use a thread for the timer). It is a good write up and they include the why.
Hope this helps.
I finally find a solution with sendEmptyMessageDelayed(ALIVE,10000)
Long story short, i call once ALIVE from my UI thread and at the end of the sendAlive() method i'm sending a delayedMessage to the thread itself to re-call ALIVE after X milliseconds.
With this solution no need of a new Thread or timer.
I have a main menu with an action bar. On create, I run a thread that hits my server for a current status. When Complete, the thread calls a handler which kicks off a constantly running thread that cycles through the items and uses another handler call to change the test in the actionbar. The problem is that when I change views, I either get android.view.WindowLeaked or View not attached to window manager
Here is some sample code
public class MainMenuActivity extends ProtectedWithActionBarActivity{
private int STATUS_COUNTER;
private final int RESULT_STATUS_LOADED = 2000;
private final int RESULT_SHOW_STATUS = 2001;
private CurrentStatusModel currentStatus;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu);
ActionBar footerbar = (ActionBar)findViewById(R.id.footerbar);
footerbar.setTitle("Currently connected to " + PreferencesHelper.getCurrentEnvironment().name());
STATUS_COUNTER = 0;
statusLoadThread.start();
}
Thread statusLoadThread = new Thread()
{
#Override
public void run()
{
//set currentStatus with data from server
}
};
Thread statusDisplayThread = new Thread()
{
int sleep = 5000;
boolean threadDone = false;
public void done()
{
threadDone = true;
}
#Override
public void run()
{
while(true)
{
//pick message to send to handler
//increment STATUS_COUNTER or reset to 0 when out of bounds
try
{
sleep(sleep);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
private Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
switch(msg.what)
{
case RESULT_STATUS_LOADED:
statusDisplayThread.start();
break;
case RESULT_SHOW_STATUS:
ActionBar footerbar = (ActionBar)findViewById(R.id.footerbar);
String message = ((Object[])msg.obj)[0].toString();
OnClickListener listener = (OnClickListener)((Object[])msg.obj)[1];
footerbar.setTitle(message);
footerbar.setOnTitleClickListener(listener);
break;
case ActivityBase.RESULT_ERROR:
break;
}
}
};
}
I'm not sure if what I'm doing is just wrong or if there is something blatantly obvious that I am missing. What needs to happen is the threads need to stop any time I change screens. Should I use Thread.interrupt(); before starting the next activity?
AsyncTasc allows you to implement doInBackground(), where your thread can crank away at its task. This is similar to the functionality you'd get from Thread.
The real magic happens when you override onPreExecute() and onPostExecute(), which are both executed on the UI thread. This should keep you from getting messages about your Activity not being attached.
Edit - this answer contains a small code example for AsyncTask that could get you started.
You are trying to update UI elements after the owning Activity has been detached from the windowing system.
You will make your life a lot simpler if you use AsyncTask instead of vanilla threads (no handler needed, for one thing) and cancel() the background tasks from your Activity.onPause().
Can't you set a flag in onPause that each of your Threads checks for? If the flag is set then the thread drops out of its loop. Thus whenever the Activity is moved to the background each of your Threads will stop. You would need to handle restarting the threads in onResume. You could alternatively use the AsyncTask approach, but this is not guaranteed to actually cancel when you call its cancel() method, it only attempts to cancel the task.