I want to present bitmap images from a internetstream. Every 500 millisec i get a new image and i want to present this image to the user. What is the best way to do this? Can i use an image view and chenge the image every 500 millisec?
I tried to do this in an timer task like this:
timer.schedule(new TimerTask() {
public void run() {
if(flag){
Bitmap bmp = null;
Log.i(APPID, "New frame");
try {
bmp = session.getImage();
setImage(bmp);
} catch (Exception e) {
e.printStackTrace();
}
} else {
timer.cancel();
}
}
}, 500, 500);
But this does not work.
Updating the UI from a thread other than the UI/main thread will fail as Android does not allow it. Try using a Handler to post messages back to the UI thread. You could do something like this.
final Handler h = new Handler();
timer.schedule(new TimerTask() {
public void run() {
if(flag){
h.post(new Runnable() {
public void run() {
Bitmap bmp = null;
Log.i(APPID, "New frame");
try {
bmp = session.getImage();
setImage(bmp);
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
timer.cancel();
}
}
}, 500, 500);
Timer task runs on a different thread. you need to update ui on the ui thread. You should use runOnUiThread or Handler
runOnUiThread(new Runnable() //run on ui threa
{
public void run()
{
// update ui here
}
});
Handler
Handler m_handler;
Runnable m_handlerTask ;
m_handler= new Handler();
m_handlerTask = new Runnable()
{
#Override
public void run() {
// do soemthing
m_handler.postDelayed(m_handlerTask, 1000);
// change 1000 to whatever you want
}
};
m_handlerTask.run();
When you wan to cancel call this m_handler.removeCallbacks(m_handlerTask).
Related
I have a server giving me live data in JSON format which updates every second. I have to display that in my android app.
I am a beginner and I tried Async Task updating every second via a thread and setting a delay on it.
Thread t = new Thread() {
#Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(1000);
runOnUiThread(new Runnable() {
#Override
public void run() {
// Perform the HTTP request for data and process the response.
counterAsyncTask task=new counterAsyncTask();
task.execute(REQUEST_URL);
}
});
}
} catch (InterruptedException e) {
}
}
};
t.start();
It runs out of memory and crashes after some time
Are there any alternates?
Try putting your code into handler thread
Runnable runnable = new Runnable() {
#Override
public void run() {
mHandler.postDelayed(this, 1000);
counterAsyncTask task=new counterAsyncTask();
task.execute(REQUEST_URL);
}
};
// start it with:
mHandler.post(runnable);
my_text.setText("Dave");
//Small pause...
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
my_text.setText("Bob");
I want to change my textView, pause 1 second, then change it again. When I run the program, it doesn't refresh after first change. It just shows the second change after returning. How can I force the refresh on the first change to the textview?
Try use Handler like below code
my_text.setText("Dave");
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
my_text.setText("Bob");
}
});
}
}, 1000);
Try below for the edited question
Keep your all 20 names in any collection array or list.
int position = 0;
String names [] = {"0","1","20"};
Handler handler = new Handler(getMainLooper());
Runnable runnable = new Runnable() {
public void run() {
my_text.setText(names[position++]);
if (position < names.length) {
handler.postDelayed(this, 1000);
}
}
};
handler.postDelayed(runnable, 1000);
I created one handler for calling AsyncTask in background when I start the application its working fine when I re-enter into the application it will call two times in for first one and present one, how can I handle that
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
checkandsave();
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 300000); //execute in every 60000 ms = 1 minute
i have this Handler:
private Runnable mRunnableReset = new Runnable() {
public void run() {
Log.i("MyApp", "mRunnableReset");
}
};
private Handler mHandler = new Handler();
private Runnable mRunnable = new Runnable() {
public void run() {
while (true) {
try {
Thread.sleep(2000);
mHandler.post(mRunnableReset);
} catch (Exception e) {
// TODO: handle exception
}
}
}
};
I tried to use this code in my onDestroy() method
mHandler.removeCallbacks(mRunnableReset);
mHandler.removeCallbacksAndMessages(null);
but the mRunnable is still running (i can see the log in LogCat).
If I use mRunnable.destroy() my app crashes.
How can i stop this ?
Thank you.
Instead of using Thread.sleep() use postDelayed() while posting your Runnable (instead of regular post()).
Whoops you are using
while(true){
Thread.sleep(2000);
mHandler.post(mRunnableReset);
}
How will it stop then? It won't.
Instead it should be like,
private Runnable mRunnableReset = new Runnable() {
public void run() {
Log.i("MyApp", "mRunnableReset");
mHandler.removeCallbacks(mRunnableReset);
mHandler.post(mRunnableReset);
}
};
private Handler mHandler = new Handler();
private Runnable mRunnable = new Runnable() {
public void run() {
try {
Thread.sleep(2000);
mHandler.post(mRunnableReset);
} catch (Exception e) {
// TODO: handle exception
}
}
};
And then to stop your mRunnableReset use mHandler.removeCallbacks(mRunnableReset);
And better would be using mHandler.postDelayed(mRunnableReset, 2000) instead of Thread.sleep(2000);
Also check my answer here to get complete working of how Handler will work.
I need a thread (it does httppost ,and parse the answer xml and refresh listview to set the changes from parsed xml) in 3 sec interval
I have already tried this code
Timer timer = new Timer();
timer.scheduleAtFixedRate(
new TimerTask() {
public void run() {
try {
httpPostList(url);
saxParseList();
list.invalidateViews();
Thread.sleep(1000);
} catch (Exception ie) {
}
}
}, 1000, 1000 * 30);
I would appreciate you to create a Service with an AsyncTask in it.
Async Tasks are the Android Synonym to normal Java Tasks, Documentation finding here: http://developer.android.com/reference/android/os/AsyncTask.html
Services are Background Processes, seeing this Doc:
http://developer.android.com/reference/android/app/Service.html
Try using handlers:
Handler handler;
#Override
public void onCreate(Bundle savedInstanceState) {
// ...
handler = new Handler() {
#Override
public void handleMessage(Message msg) {
updateUI();
}
};
Thread thread = new Thread() {
#Override
public void run() {
while(true) {
Message msg = new Message();
handler.sendMessage(msg);
try {
sleep(3*1000); // 3 seconds
} catch (InterruptedException e) {
}
}
}
};
thread.start();
}
private synchronized void updateUI() {
// ...
}
Finally I made it using "Async task".