Android: How to run a Task inside another Task (TimerTask) - android

I have the following code:
//Task that runs in background thread and posts results
private class NewsWorkerTask extends AsyncTask<Void, Void, List<NewsData>> {
#Override
protected void onPreExecute() {
}
#Override
protected List<NewsData> doInBackground(Void... params) {
if (NewsDataProvider==null || NewsDataProvider.PageNumber ==0)
{
//Get New Data and initialize
NewsDataProvider = new NewsProvider(getActivity());
return NewsDataProvider.GetTopNews();
}
else
{
List<NewsData> tempDataList = NewsDataProvider.GetTopNews();
// Merge new page
for (NewsData item : tempDataList) {
TopNewsDataList.add(item);
}
}
return null;
}
/*
* The system calls this to perform work in the UI thread and delivers
* the result from doInBackground()
*/
#Override
protected void onPostExecute(List<NewsData> data) {
final List<NewsData> tempDataList = data;
//Declare the timer
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
//Called each time when 1000 milliseconds (1 second) (the period parameter)
if (tempDataList != null && tempDataList.size() > 0) {
if (tempDataList.size() == 1) {
TextView txtNewsTitle = (TextView)getView().findViewById(R.id.txtNewsTitle);
TextView txtNewsDate = (TextView)getView().findViewById(R.id.txtNewsDate);
txtNewsTitle.setText(tempDataList.get(0).DESCRIPTION);
txtNewsDate.setText(tempDataList.get(0).NEWS_DATE);
}
else {
TextView txtNewsTitle = (TextView)getView().findViewById(R.id.txtNewsTitle);
TextView txtNewsDate = (TextView)getView().findViewById(R.id.txtNewsDate);
txtNewsTitle.setText(tempDataList.get(newsIndex).DESCRIPTION);
txtNewsDate.setText(tempDataList.get(newsIndex).NEWS_DATE);
newsIndex++;
if (newsIndex == (tempDataList.size() - 1)) {
newsIndex = 0;
}
}
}
}
},
//Set how long before to start calling the TimerTask (in milliseconds)
0,
//Set the amount of time between each execution (in milliseconds)
5000);
}
}
As you can see the TimerTask runs in the onPostExecute method of the NewsWorkerTask
I get the following error when I do this:
FATAL EXCEPTION: Timer-0
android.view.ViewRootImpl$CalledFromWrongThreadException
Only the original thread that created a view hierarchy can touch its views.
The reason I put the timer in the onPostExecute is because I need to execute the timer when I get the Data (GetTopNews())
GetTopNews basically gives me the top 10 latest news I want to display them inside a box that switches to the next news every 5 seconds.

try using
yourview.post(new Runnable() {
public void run() {
//change your defined view here
}
});
inside timertask and update views inside the function above!

Related

Update TextView every second in Android [duplicate]

This question already has answers here:
Update TextView Every Second
(11 answers)
Closed 4 years ago.
i want to update my textview every second.
on button click i am calling one method,
loopMethod(milli); //suppose milli= 50000 i.e 50 sec.
so my loopMethod(int m) is as follows:
public void loopMethod(int m){
timer=(TextView) findViewById(R.id.timerText);
if(m>=1000){
try {
timer.setText(""+m);//timer is a textview
System.out.println(m);
m=m-1000;
Thread.sleep(1000);
} catch(InterruptedException ex) {
ex.printStackTrace();
}
loopMethod(m);
}
}
so what i am expecting is, my timer textview should print the value of m every second.
but i am getting only console output i.e system.out.println(m)...
printing value on console working fine...
but its not updating my textview at all
You can use following code:
Runnable updater;
void updateTime(final String timeString) {
timer=(TextView) findViewById(R.id.timerText);
final Handler timerHandler = new Handler();
updater = new Runnable() {
#Override
public void run() {
timer.setText(timeString);
timerHandler.postDelayed(updater,1000);
}
};
timerHandler.post(updater);
}
In this line:
timerHandler.post(updater);
time will set for the first time. i.e, updater will execute. After first execution it will be posted after every 1 second time interval. It will update your TextView every one second.
You need to remove it when the activity destroys, else it will leak memory.
#Override
protected void onDestroy() {
super.onDestroy();
timerHandler.removeCallbacks(updater);
}
Hope it will help you.
You should use RxJava library to do so:
Subscription s =
Observable.interval(1, TimeUnit.SECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(v -> {
// update your ui here
}, e -> {
});
// call when you no longer need an update:
if (s != null && !s.isUnsubscribed()){
s.unsubscribe();
s = null;
}
That's it. Do NOT use .postDelay(), Timer because it is error prone.
You might want to consider using the Chronometer class: https://developer.android.com/reference/android/widget/Chronometer.html
just use timer.start(); on the button click
Using handler can be used like this
TextView timer;
int m =0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timer=(TextView) findViewById(R.id.timerText);
Handler handler = new UpdateHandler();
m = 10;
handler.sendEmptyMessageDelayed(1, 1000);//start after 1000
}
class UpdateHandler extends Handler{
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1:
timer=(TextView) findViewById(R.id.timerText);
timer.setText("Text :" +m);
m = m-1000;
sendEmptyMessageDelayed(1, 1000); //seng again after 1000
//add some stop logic
break;
default:
break;
}
}
}
Try this code Initialize textview in
onCreate
timer=(TextView) findViewById(R.id.timerText);
public void loopMethod(int m){
if(m>=1000){
try {
System.out.println(m);
m=m-1000;
final ScheduledThreadPoolExecutor c = new ScheduledThreadPoolExecutor(1);
c.schedule(new Runnable() {
#Override
public void run() {
timer.setText(""+m);//timer is a textview
c.shutdownNow();
}
}, 1, TimeUnit.SECONDS);
} catch(InterruptedException ex) {
ex.printStackTrace();
}
loopMethod(m);
}
}
I've added some logics to stop the Timer. If you have any qyestion, ask freely
private int m = 0;
private int milliseconds = 1000;
public void loopMethod(int m){
timer=(TextView) findViewById(R.id.timerText);
Timer t = new Timer();
//schedule a timer
t.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
timer.setText(String.valueOf(m));//avoid using composite string in the setText
System.out.println(String.valueOf(m));
//remove from the total the amount of millisecond passed
m=m-milliseconds;
if(m <= milliseconds) { //or <= what you want
//stop the timer repeatitions
t.cancel();
}
}
});
}
//"0" is the amount of time to wait for the timer to start
//"milliseconds" is the duration
},0,milliseconds);
}
Add
For a correct analysis you should add more infos in your question. the problem of not-updating textview might be caused by the setText("" + int) because it's always better to avoid the setText with an int. I edited it with String.valueOf, but if it's not working you should add the xml and the onCreate
Hope this helped
I have created timer for seconds.
public class TimerForSeconds extends AppCompatActivity {
private int seconds = 60;
private TextView tvTimer;
private Handler mHandler;
private Runnable runnable = new Runnable() {
#Override
public void run() {
if(seconds == 0){
mHandler.removeCallbacks(runnable);
}
else{
tvTimer.setText(seconds + "");
seconds--;
mHandler.postDelayed(runnable,1000);
}
}
};
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_activity);
tvTimer = (TextView) findViewById(R.id.tv_timer);
mHandler = new Handler();
mHandler.postDelayed(runnable,1000);
}
}
//and also removCallback onDestroy too.

How to get runOnUiThread to run continuously in Android

Using Android studio, I am trying to make an app that gets data from a web-service and display the data and updates the view every 5 sec or when the data on the web-service changes. With this I am trying to change the colours on some button based on an int, the int changes and the color on the button changes when I apply buttons(); to another button and then presses it but I want it to update by itself.
When I used a while loop the app gets stuck
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
buttons();
}
});
The runOnUiThread is placed in the onCreate.
using run on UI thread will cause your UI to freeze , try using a timer task instead .
example :
#Override
public void StopTimerTask() {
if (timer != null) {
timer.cancel();
timer = null;
}
}
public void StartTimer() {
timer = new Timer();
initializeTimerTask();
int UpdateTime = Integer.valueOf(UserSettings.getString("Update", "60000"));
timer.schedule(doAsynchronousTask, 0, YOURTIME);
}
public void initializeTimerTask() {
doAsynchronousTask = new TimerTask() {
#Override
public void run() {
myHandler.post(new Runnable() {
public void run() {
YOUR LOGIC HERE
}
});
}
};
}
doing this where u just put another class into main activity was succesful only problem is that it have to be in my main class
public class updategui extends TimerTask {
Activity context;
Timer timer;
public updategui(Activity context, int seconds) {
this.context = context;
timer = new Timer();
timer.schedule(this,
seconds * 1000, // initial delay
seconds * 1000); // subsequent rate
}
#Override
public void run() {
if(context == null || context.isFinishing()) {
this.cancel();
return;
}
context.runOnUiThread(new Runnable() {
#Override
public void run() {
try {
buttons();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}}

Use AsyncTask Except Downloading

Wherever I've seen use of asynctask , it is used for downloading or operation that returns some results. But What if i just want to set a time to time updation of my TextView in activity and do some work at the end .
But at the same time How can i cancel asynctask operation from activity so its onPostExecute , do not run .Like when back button is pressed from activity. Any clues ?
[UPDATE]
For Time to time updation i mean:
TextView tv = findViewById(R.id.disco);
try{
for(int i=0;i<10000;i++){
Thread.sleep();
tv.setText(" "+i);
}
}
catch(Exception e){}
/* i know i can achieve finally in onPostExecute but what if i want to cancel it during runtime*/
finally{
// do some more operations after execution
}
Done it myself after some brain storming and searching , i am using CountDownTimer.
in OnCreate():
counter = new CountDownTimer((PROGRESSSECONDS+1)*1000,1000) {
int collapsed = 0;
#Override
public void onTick(long millisUntilFinished) {
collapsed++;
pb.setProgress(collapsed);
}
#Override
public void onFinish() {
Intent in = new Intent(FirstActivity.this,PointsDrawerActivity.class);
startActivity(in);
}
};
counter.start();
in onBackPressed():
#Override
public void onBackPressed() {
counter.cancel();
counter = null;
setContentView(R.layout.activity_first);
}
it worked.
Handler is optimum for your requirement
handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
callMethod();
handler.postDelayed(this, 1000);
}
};
handler.postDelayed(r, 1000);
and in order to cancel an ongoing AsyncTask the official docs says it all
A task can be cancelled at any time by invoking cancel(boolean).
Invoking this method will cause subsequent calls to isCancelled() to
return true.
After invoking this method, onCancelled(Object), instead of onPostExecute(Object) will be invoked after doInBackground(Object[])
returns.
To ensure that a task is cancelled as quickly as possible, you should always check the return value of isCancelled() periodically
from doInBackground(Object[]), if possible (inside a loop for
instance.)
Ex: MyTask.cancel(true);
You should use timer task rather than Async task here.
Here is sample:
private TimerTask timerTask;
int i = 0;
timerTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
//Do your text view update here.
tv.setText(" "+ (i++));
}
});
}
};
In your onResume() do like:
private Timer timer;
public void onResume() {
timer = new Timer();
timer.schedule(timerTask, 1000); // time in milliseconds, you can set accordingly requirement.
}
And onPause() you can stop it by:
public void onPause() {
if (timer != null) {
timer.cancel();
timer = null;
}
}

Android: Nested TimerTask not stopping

I have some test code with nested TimerTasks, i.e. one task is called every 10s to change parameters and it starts another timer running some calcs every 100ms or so. When I call cancel on the first timer it cancels the outer TimerTask but the inner one keeps running.
What do I have to do to get the inner task stopped too? Note that in the following code the cancel() override never gets called, although the outer task does stop.
class CalcTask extends TimerTask
{
// randomly changes calc interval every time it is called
public void run(){
if (mTmr != null)
mTmr.cancel();
mTmr = null;
TimerTask calcTask = new TimerTask(){ public void run(){ Log.i("TempSensor","chad"); doCalcs(); } };
int min = Integer.parseInt(mEditThreadMin.getText().toString());
int max = Integer.parseInt(mEditThreadMax.getText().toString());
int interval = mRand.nextInt(max-min)+min + 1;
mTmr = new Timer(true);
mTmr.schedule(calcTask,0,interval);
}
#Override
public boolean cancel()
{
Log.i("TempSensor","Cancelling");
if(mTmr != null)
{
mTmr.cancel();
mTmr.purge();
}
return super.cancel();
}
private Random mRand = new Random();
private Timer mTmr = null;
};
... // later on
mTimerCalc1.scheduleAtFixedRate(new CalcTask(), 0, (int)10e3);

Android: TimerTask scheduled for repetition getting fired only once

Ok this is a very weird problem I am having, and I'm pretty sure that I am messing up somewhere, but I can't quite figure out where.
What I am trying is -
Schedule a Timer to execute a TimerTask every five seconds
The TimerTask in turn executes an AsyncTask (which in this case simple sleeps for a second before returning the static count of the number of AsyncTasks).
Finally, the aforementioned count is updated in the UI.
And of course, the appropriate Handlers and Runnables have been used to post asynchronous messages from other threads to the UI.
This code executes only once. I expect it to fire every 5 seconds. Here's the code.
Note: I had no idea what to do with the Looper. I put it there after trial and error!
public class TimerAsyncMixActivity extends Activity {
public static final String TAG = "TimerAsyncMix";
static int executionCount = 0;
Handler mHandler = new Handler();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Timer().schedule(new MyTimerTask(this), 0, 5000);
}
class MyAsyncTask extends AsyncTask<String, Void, Integer>{
#Override
protected Integer doInBackground(String... params) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ++executionCount;
}
#Override
protected void onPostExecute(Integer result) {
mHandler.post(new UpdateUiThread(TimerAsyncMixActivity.this, result));
super.onPostExecute(result);
}
}
}
class MyTimerTask extends TimerTask{
private TimerAsyncMixActivity tma;
public MyTimerTask(TimerAsyncMixActivity tma) {
this.tma = tma;
}
#Override
public void run() {
Looper.prepare();
Log.d(TimerAsyncMixActivity.TAG, "Timer task fired");
tma.new MyAsyncTask().execute();
Looper.loop();
Looper.myLooper().quit();
}
}
class UpdateUiThread implements Runnable{
int displayCount;
TimerAsyncMixActivity tma;
public UpdateUiThread(TimerAsyncMixActivity tma, int i) {
this.displayCount = i;
this.tma = tma;
}
#Override
public void run() {
TextView tv = (TextView) tma.findViewById(R.id.tvDisplay);
tv.setText("Execution count is : "+displayCount);
}
Can anyone point me to what I'm doing wrong?
techie, this is how I implemented similar things. I'm won't claim that this is the best way, but it has worked for me and doesn't look too bad.
I have the following code in my activity. I create an async task when the activity starts and I stop it onPause. The AsyncTask does whatever it needs to do, and updates the UI on onProgressUpdate() (which is run on the UI thread, so there's no need to use a Handler).
private Task task;
#Override
protected void onPause() {
task.stop();
task = null;
}
#Override
protected void onResume() {
task = new Task();
task.execute();
}
private class Task extends AsyncTask<Void, String, Void> {
private boolean running = true;
#Override
protected Void doInBackground(Void... params) {
while( running ) {
//fetch data from server;
this.publishProgress("updated json");
Thread.sleep(5000); // removed try/catch for readability
}
return null;
}
#Override
protected void onProgressUpdate(String... values) {
if( ! running ) {
return;
}
String json = values[0];
//update views directly, as this is run on the UI thread.
//textView.setText(json);
}
public void stop() {
running = false;
}
}
Do not use a timer. If your phone goes to sleep, the timer is suspended too. Use AlarmManager.

Categories

Resources