I just want to continously update text in an android app. However my App crashes every time.
This is my code:
package org.pgvw.main;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class InertialView extends Activity {
TextView tv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
tv.setText("Time: ");
new Thread(new Runnable() {
public void run() {
while(true) {
try {
Thread.currentThread().sleep(100);
tv.setText("Time: " + System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
setContentView(tv);
}
}
Can anyone spot the mistake?
Greetings and thx!
Consider that you do not need to launch any additional threads to update the UI every x milliseconds. The additional overhead of an added thread is justified IF you have a time intensive task. In the following example the timer is launched on a button click. Note that this code does not create a new thread.
private Handler myHandler= new Handler(){
#Override
public void handleMessage(Message msg){
switch(msg.what){
case 0:
if (!isDoneThinking){
editTextConfusedText.setText("Still Thinking "+new Integer(thinkSeconds).toString());
thinkSeconds++;
this.removeMessages(0);
sendMessageDelayed(obtainMessage(0),1000); // <== Loop on delayed messages every second
}
else {
thinkSeconds= 0; // reset timer
}
break;
default:
super.handleMessage(msg);
break;
}
}
};
We launch the timer in onClick. We simply send an empty message with a what value of "0".
buttonConfuseText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
isDoneThinking= false;
myHandler.sendEmptyMessage(0); <== starts timer with what value of "0"
}
};
The timer is unconstrained and will continue to count until the flag isDoneThinking is set to true.
You cannot update GUI from another thread. You can use runOnUiThread or handler to update the GUI.
for example:
new Thread(new Runnable() {
public void run() {
while(true) {
try {
Thread.currentThread().sleep(100);
runOnUiThread(new Runnable() {
public void run() {
tv.setText("Time: " + System.currentTimeMillis());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
BTW - The use of Thread.currentThread().sleep(100); can be shorted to Thread.sleep(100) since it's a static method. see Jon Skeet's answer regarding that
Or use AsyncTask. It was designed to do background tasks and properly update the UI. Your design will be more scalable this way.
Related
I have a textview which i want to change with a thread and do it again and again (Like a digital clock). But i'm having problems with setting time between 2 changes. Here, the code:
display1 = (TextView) findViewById(R.id.textView1);
Thread timer2 = new Thread(){
#Override
public void run() {
synchronized (this){
runOnUiThread(new Runnable() {
#Override
public void run() {
int i = 0;
display1.setText("" + i);
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
display1.setText("" + (i+1));
}
});
}
}
};
timer2.start();
This sleep(2000); function makes textview invisible for the given time but i want it stand still till the next change. How can i do that?
But i'm having problems with setting time between 2 changes
Do NOT do sleep() on your UI thread. If you want to chain some actions with the delay, split your code into two runnables and the first one should set display1 and then post second runnable with the delay using postDelayed()
EDIT
want one of them to increase 3 per sec, and the other 5 per sec until they reach 1000 for instance
You can make your Runnable post itself until some criteria are met (i.e. time, counter value etc). Just at the end your Runnable check your conditions and if not met, call postDelayed(this, delay); and you are good.
You should use a Handler instead and dispatch your changes to the UI thread.
Handler mHandler = new Handler();
mHandler.post(new Runnable() {
#Override
public void run() {
// Code to be run right away
}
});
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
// Code to be run after 2 seconds
}
}, 2000);
Maybe split up what you need to do into separate methods. Here you have a UI method and a sleep method split up. Hope it helps
private void myMethod() {
new Thread() {
#Override
public void run() {
int i = 0;
doWorkOnUI(String.valueOf(i));
pause(2000);
doWorkOnUI(String.valueOf(i++));
}
}.start();
}
private void pause(int pauseTime) {
try {
Thread.sleep(pauseTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void doWorkOnUI(final String string) {
runOnUiThread(new Runnable() {
#Override
public void run() {
display1.setText(string);
}
});
}
I have one textview, which i can use to show countdown timer. I wanted to use Thread class which takes Runnable interface.
I wrote the following code for the same but it is gives run time error Unfortunately ThreadApp has Stopped
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity implements Runnable
{
TextView tvTimer;
Thread timerThread;
int time=30;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvTimer =(TextView)findViewById(R.id.textView1);
timerThread= new Thread(this);
timerThread.start();
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void run()
{
try
{
Thread.sleep(1000);
tvTimer.setText((String.valueOf(time)).toString());
time--;
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
At the same time when i removed tvTimer.setText((String.valueOf(time)).toString()); it works fine. Can anyone provide me the solution. I am new in android.
You can use updating UI using RunOnUiThread like this
#Override
public void run()
{
try
{
Thread.sleep(1000);
runOnUiThread(new Runnable() {
public void run() {
tvTimer.setText((String.valueOf(time)).toString());
}
});
time--;
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
when i removed tvTimer.setText((String.valueOf(time)).toString()); it
works fine
because you are trying to change TextView text from non-ui Thread so use runOnUiThread or Handler for updating TextView from other Thread
new Thread(new Runnable() {
#Override
public void run() {
Thread.sleep(1000);
runOnUiThread(new Runnable() {
public void run() {
tvTimer.setText((String.valueOf(time)).toString());
time--;
}
});
}
You are trying to update ui in timer task. Timer task runs on a different thread. update ui on the ui thread. Use a handler or runonuithread
Handler m_handler;
Runnable m_handlerTask ;
m_handler = new Handler();
m_handlerTask = new Runnable()
{
#Override
public void run() {
tvTimer.setText((String.valueOf(time)).toString());
timer--;
// do something
m_handler.postDelayed(m_handlerTask, 1000); // instad of 1000 mention the delay in milliseconds
}
};
m_handlerTask.run();
Or use
#Override
public void run()
{
try
{
Thread.sleep(1000);
// should not call thread.sleep() bad design
// check the edit below
runOnUiThread(new Runnable(){
#Override
public void run(){
//update ui here
tvTimer.setText((String.valueOf(time)).toString());
}
});
time--;
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Edit: Using Thread.sleep() inside a thread is a bad design.
http://developer.android.com/training/articles/perf-anr.html
Quoting from the docs.
If you implement Thread or HandlerThread, be sure that your UI thread does not block while waiting for the worker thread to complete—do not call Thread.wait() or Thread.sleep(). Instead of blocking while waiting for a worker thread to complete, your main thread should provide a Handler for the other threads to post back to upon completion. Designing your application in this way will allow your app's UI thread to remain responsive to input and thus avoid ANR dialogs caused by the 5 second input event timeout.
To stops use this m_handler.removeCallbacks(m_handlerTask);
You are trying to update the UI element i.e., a TextView in your thread. You can't do this in a thread. Instead use a handler to post changes to your UI element as follows -
Handler handler = new Handler();
#Override
public void run()
{
try
{
Thread.sleep(1000);
handler.post(new Runnable()
{
public void run()
{
tvTimer.setText((String.valueOf(time)).toString());
}
time--;
});
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I am trying to make a clock, using a TextView :)
Someone here told me that I couldn't use normal threads to change the UI, but Handler or AsyncTask. I managed to get it working a few days ago, but was not a consistent thread.
Now what I want is a consistent thread that is always changing the text of my Textview. I tried using this, but didn't work, any help?
private void startClock() {
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
while (true) {
final long millis = System.currentTimeMillis() - MainActivity.startedAt;
clock.setText("" + millis);
runOnUiThread (new Runnable() {
#Override
public void run() {
clock.setText("" + millis);
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}, 2000);
}
you should get rid of:
while(true) {
....
sleep(1000);
...
}
because this get your thread stuck forever. your program should work like this:
private Handler mHandler = new Handler();
#Override
public void onResume() {
super.onResume();
mHandler.removeCallbacks(mUpdateClockTask);
mHandler.postDelayed(mUpdateCLockTask, 100);
}
#Override
public void onPause() {
super.onPause();
mHandler.removeCallbacks(mUpdateClockTask);
}
private Runnable mUpdateClockTask = new Runnable() {
public void run() {
updateClock();
mHandler.postDelayed(mUpdateClockTask, 2000);
}
};
and inside updateClock() you do all your UI updates.
Look here for an example https://stackoverflow.com/a/11140429/808940
Also note that you have a duplicate line in your code:
clock.setText(""+millis);
It appears both in the runOnUiThread and in the main handler, it should only appear in the runOnUiThread runnable
The problem is that when i click the button for the second my application crashes and i don't understand why. When i clink the button for the second time is suppose to the same all over again.
Any help is appreciated.
public class main extends Activity {
Boolean grabar = false;
TextView texto;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
texto = (TextView) findViewById(R.id.texto);
Button startbtn = (Button) findViewById(R.id.button);
startbtn.setOnClickListener(new OnClickListener(){
public void onClick(View v){
background.start();
}
});
}
Thread background = new Thread (new Runnable() {
public void run() {
try {
Thread.sleep(3000);
cambiarHandler.sendEmptyMessage(0);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Handler cambiarHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
texto.setText("ok");
}
};
}
The problem I see is that threads are designed to run once. You need to create a new instance of the thread each time you wish to run it.
This may well have something to do with Android's policy of only allowing the UI thread to modify the UI.
You should be using ASyncTask instead if you want to modify the UI from a separate thread.
See here:
Update UI from Thread
I don't know which exception is thrown. I guess it is NPE thrown. When you try to set text to the TextView. Try instantiating the TextView within the handleMessage() method:
Handler cambiarHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
texto = (TextView) findViewById(R.id.texto);
texto.setText("ok");
}
};
And in the onClick method do this to have new instance of the thread each time button is clicked.
public void onClick(View v){
new Thread (new Runnable() {
public void run() {
try {
Thread.sleep(3000);
cambiarHandler.sendEmptyMessage(0);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
I have to display some data after every 10 seconds. Can anyone tell me how to do that?
There is an another way also that you can use to update the UI on specific time interval. Above two options are correct but depends on the situation you can use alternate ways to update the UI on specific time interval.
First declare one global varialbe for Handler to update the UI control from Thread, like below
Handler mHandler = new Handler();
Now create one Thread and use while loop to periodically perform the task using the sleep method of the thread.
new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
Thread.sleep(10000);
mHandler.post(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
// Write your code here to update the UI.
}
});
} catch (Exception e) {
// TODO: handle exception
}
}
}
}).start();
Probably the simplest thing to do is this:
while(needToDisplayData)
{
displayData(); // display the data
Thread.sleep(10000); // sleep for 10 seconds
}
Alternately you can use a Timer:
int delay = 1000; // delay for 1 sec.
int period = 10000; // repeat every 10 sec.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
displayData(); // display the data
}
}, delay, period);
Andrahu was on the right track with defining a handler. If you have a handler that calls your update functions you can simply delay the message sent to the handler for 10 seconds.
In this way you don't need to start your own thread or something like that that will lead to strange errors, debugging and maintenance problems.
Just call:
Handler myHandler = new MyUpdateHandler(GUI to refresh); <- You need to define a own handler that simply calls a update function on your gui.
myHandler.sendMessageDelayed(message, 10000);
Now your handleMessage function will be called after 10 seconds. You could just send another message in your update function causing the whole cycle to run over and over
There is Also Another way by Using Handler
final int intervalTime = 10000; // 10 sec
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Display Data here
}
}, intervalTime);
There is a Simple way to display some data after every 10 seconds.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
ActionStartsHere();
}
public void ActionStartsHere() {
againStartGPSAndSendFile();
}
public void againStartGPSAndSendFile() {
new CountDownTimer(11000,10000) {
#Override
public void onTick(long millisUntilFinished) {
// Display Data by Every Ten Second
}
#Override
public void onFinish() {
ActionStartsHere();
}
}.start();
}
Every 10 seconds automatically refreshed your app screen or activity refreshed
create inside onCreate() method i tried this code will work for me
Thread t = new Thread() {
#Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(1000);
runOnUiThread(new Runnable() {
#Override
public void run() {
//CALL ANY METHOD OR ANY URL OR FUNCTION or any view
}
});
}
} catch (InterruptedException e) {
}
}
};t.start();