Android timer with do while - android

My code is crashing and I need some help. Everything is running fine except for this part.
There is no errors but it crashes at timer.schedule(loadImg2, 5000); that is before the if (!c.moveToNext())
My question: am I using the timer correctly in the loop? Because that is where the code crashes.
I never see this log "+-+-+-+-+-+-+-+-+- Getting out " or anything that comes after.
do
{
Log.v("log_tag", "+-+-+-+-+-+-+-+-+- Ttrying to cancel ");
//timer.cancel();
Log.v("log_tag", "+-+-+-+-+-+-+-+-+- Timer canceled ");
timer = new Timer();
Log.v("log_tag", "+-+-+-+-+-+-+-+-+- New timer created ");
//delay amount of time(5s here) in milliseconds before first execution.
//period amount of time(1s here) in milliseconds between subsequent executions.
timer.schedule(loadImg2, 5000); //this did not produce any effect so far
if (!c.moveToNext())
{
//destroy
timer.cancel();
myImageView.setImageBitmap(null);
Log.v("log_tag", "+-+-+-+-+-+-+-+-+- Getting out ");
//get out of the loop or set c.moveToFirst()
break;
}
} while (true);
TimerTask loadImg2 = new TimerTask()
{
#Override
//Load Img2
public void run()
{
runOnUiThread(new Runnable()
{
public void run()
{
Log.v("log_tag", "+-+-+-+-+-+-+-+-+- Inside loadImg2 ");
titleText.setText(DisplayTitle(c));
Bitmap bitmap2 = BitmapFactory.decodeFile(c.getString(5));
myImageView = (ImageView) findViewById(R.id.imageview1);
myImageView.setImageBitmap(null);
timer.cancel();
}
});
}
}

There is a problem that you can't set timer.schedule() more than once time.
Try to catch an exception:
try{
timer.schedule(loadImg2, 5000);
} catch (IllegalArgumentException e){
Log.v(TAG, "IllegalArgumentException");
} catch (IllegalStateException e){
Log.v(TAG, "IllegalStateException");
}
Then the application doesn't crash. But the timer will set just in the first time.
I don't know the solution of this problem. I am trying to find it too.

Related

button.setText() and Thread.sleep()

Noob question on its way. In the code below I update text of a button in Android. Then I want to wait two seconds and then update the text again. If I comment the second b.setText("Send data"), the one after the sleep - then b.setText("Success") is written to the button. If I do not comment that one I will never see text "Success" on the button, only "Send data". It's like the Thread.sleep() is skipped when I have the second b.setText("Send data"). Google suggested to add a timer after setText("Success") so that setText() code would have time to be execited before the sleep. Did not help.
final Button b = (Button) findViewById(R.id.button);
b.setText("Send data");
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
b.setClickable(false);
b.setText("Success");
System.out.println("Debug before");
try
{
Thread.sleep(2000);
}
catch (Exception e)
{
}
System.out.println("Debug after");
b.setText("Send data");
b.setClickable(true);
}
});
Don't block your main thread. Use Handler.post instead
b.setClickable(false);
b.setText("Success");
System.out.println("Debug before");
new Handler().postDelayed(new Runnable(){
System.out.println("Debug after");
b.setText("Send data");
b.setClickable(true);
}, 2000);
There are many ways to do it.
you can run a new thread and then update view.
Or :
CountDownTimer countDownTimer = new CountDownTimer(2000, 1000) {
public void onTick(long millisUntilFinished) {
//
}
public void onFinish() {
//update your view
System.out.println("Debug after");
b.setText("Send data");
}
};
countDownTimer.start();

Timer Task Execute wrong time delay

My app started automatic when device is Restart or Manually turnoff.
in this app i am changing imageView image after every 25 second and its working fine.
But when i restart mobile device and app start automatic
then some image wait for 25 second but some images change after 1 or 2 second.
Please Help
Here is code--->
i = 0;
Timer slideTimer = new Timer();
slideTimer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
if (i >= imageBitmapList.size()){
i = 0;// resert value
}
try {
img_vw2.setImageBitmap(imageBitmapList.get(i));
}catch (Exception e){
}
i++;
}
});
}
}, 0, 25000);

How to show a counter in android UI

Can I use a thread for increment a counter and shows it in a frame of Android activity.
Public class MainActivity extendsActivity {
TextView counter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = (TextView) findViewById(R.id.TV_counter);
Thread t = new Thread() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
for (int i = 0; i < 5; i++) {
try {
counter.setText("" + i);
System.out.println("Value of i= " + i);
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
}
};
t.start();
}
}
I wrote this code, but it run properly in console, but the text view displays i=4 in the terminal, I modified the time to sleep(3000) and the problem persists.
First you don't ever want to put sleep in UI Thread that can lead to unresponsive user interface and that is never good. You should use it just to update your graphics. Try replacing your code with this
Thread t = new Thread() {
public void run() {
for (int i = 0; i < 5; i++) {
try {
final int a = i;
runOnUiThread(new Runnable() {
public void run() {
counter.setText("" + a);
}
});
System.out.println("Value of i= " + i);
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t.start();
You are going to notice that sleep and for loop is outside UIThread and in your first thread, so basically all of your math is done outside and you just display the results.
This is just a correction of your code and suggestion for further thinking
EDIT: And for you to better understand why your code is not working, you set some value on your TextView, and immediately after you set UIThread to sleep, UIThread blocks instead of giving it time to finish updating graphics, after he finish sleep you set new value, and he never got to update previous one so in the end you see just the last one.
Hope this helps and enjoy your work.
you can use a CountDownTimer, and update your UI in the onTick() method ( this method is executed on the UI Thread):
int i=0;
CountDownTimer timer = new CountDownTimer(5000,1000) {
#Override
public void onTick(long millisUntilFinished) {
// this method will be executed every second ( 1000 ms : the second parameter in the CountDownTimer constructor)
i++;
txt.setText(i);
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
}
};
timer.start();

Android present streamed images

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).

Trying to get a CountDownTimer to restart after 15 seconds.

I'm doing an app that displays various yoga poses for 60 seconds. I've been pulling my hair out trying to get a countdown timer to work the way I want. I need the timer to be able to pause, and restart automatically after 15 seconds. The CountDownTimer from Android didn't work because it had no pause. I tried 2 different versions of rewritten Android CountDownTimers and the classic version (Android CountDownTimer) and they all seem to have the same bug because they are using Handlers.
I've tried this version: http://www.java2s.com/Open-Source/Android/Timer/multitimer-android/com/cycleindex/multitimer/CountDownTimerWithPause.java.htm
I've tried this version: Android: CountDownTimer skips last onTick()! - I added an onFinish method to it.
I'm trying to do a Thread.sleep in the onFinish method for 15 seconds, and it is not updating the UI past the last second before moving into the Thread.sleep mode. In the following code, the TextView isn't getting updated to be set to empty string until after the Thread.sleep call.
cdt = new CountDownTimerWithPause(60000,1000,true) {
#Override
public void onTick(long millisUntiFinished) {
tvClock.setText("Remaining:" + millisUntiFinished/1000);
}
#Override
public void onFinish() {
tvClock.setText("");
bell.start();
if (bAutoPlayIsOn) {
tvClock.setText("waiting for next pose...");
try {
//15 second pauses between poses
Thread.sleep(15000);
//start over
listener.onForwardItemSelected();
ResetClocktoMaxTime(60000);
resume();
} catch (InterruptedException e) {
Log.d("Sleep Interrupted", e.toString());
e.printStackTrace();
}
catch (Exception e) {
Log.d("Timer", e.toString());
}
}
else {
tvClock.setText("Done");
btnStart.setText("Start ");
bClockIsTicking = false;
btnStart.setCompoundDrawablesWithIntrinsicBounds(null, playBtn, null, null);
}
}
};
Anyone have any ideas how to do this (restart after 15 seconds) differently?
The onFinish method is being called from the interface thread, so any interface changes you make won't actually redraw until onFinish returns. Try something like this:
cdt = new CountDownTimerWithPause(60000,1000,true) {
#Override
public void onTick(long millisUntiFinished) {
tvClock.setText("Remaining:" + millisUntiFinished/1000);
}
#Override
public void onFinish() {
tvClock.setText("");
bell.start();
if (bAutoPlayIsOn) {
tvClock.setText("waiting for next pose...");
(
new Thread() {
public void run() {
try {
//15 second pauses between poses
Thread.sleep(15000);
getActivity().runOnUiThread
(
new Runnable() {
public void run() {
//start over
listener.onForwardItemSelected();
ResetClocktoMaxTime(60000);
resume();
}
}
);
}
catch (InterruptedException e) {
Log.d("Sleep Interrupted", e.toString());
e.printStackTrace();
}
catch (Exception e) {
Log.d("Timer", e.toString());
}
}
}
).start();
}
else {
tvClock.setText("Done");
btnStart.setText("Start ");
bClockIsTicking = false;
btnStart.setCompoundDrawablesWithIntrinsicBounds(null, playBtn, null, null);
}
}
};

Categories

Resources