android thinking: AsyncTask in TimerTask - android

private void startUpdateTimerTask() {
TimerTask task = new TimerTask() {
#Override
public void run() {
doUpdate();
}
};
Timer timer = new Timer(true);
timer.schedule(task, ONE_MINUTE_MILLIS, ONE_HOUR_MILLIS);
}
private void doUpdate() {
new AsyncTask<Void,Void,Void>() {
#Override
protected Void doInBackground(Void... params) {
//....Network time-consuming tasks
return null;
}
}.equals();
}
(1)my question: When I run this function, there will be RuntimeException(No Looper; Looper.prepare() wasn't called on this thread.);
So I changed:
private void startUpdateTimerTask() {
TimerTask task = new TimerTask() {
#Override
public void run() {
Looper.prepare();
doUpdate();
Looper.loop()
}
};
Timer timer = new Timer(true);
timer.schedule(task, ONE_MINUTE_MILLIS, ONE_HOUR_MILLIS);
}
then RuntimeException does not appear ,but doUpdate() Executed only once?
(2) Question: How to achieve access to the network to update information every 1 hour?

then RuntimeException does not appear, but doUpdate() Executed only
once?
This is because an asynctask can execute only once.The doInBackground() runs on a separate thread, and once a thread has completed its process, you cannot start it again. Since you are already using timer task, the timer task performs operation on separate worker thread, so you can perform the same operation in the run() of timer task, which you are performing in doInBackground() of AsyncTask. For updating your UI, you can make use of Runnable.

Related

AsyncTask not called from TimerTask android

I am developing the project which continuously get datas from server at 30secs interval.So I used Handler with Timer and called Asynctask.
But my asynctask not called.This is my code,
final Handler handler;
handler = new Handler();
timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask()
{
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
try
{
System.out.println("I am xxx");
LiveTrack myActivity = new LiveTrack();
AsyncTask<String, String, String> task = myActivity.new VehiclePath();
task.execute();
}
catch (Exception e)
{
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 30000);
Can anyone guide me why I am facing this?
You cannot create an AsyncTask from within a TimerTask because it runs on a spawned thread, i.e.
not the UI thread).
AsynTasks must be created and execute()-ed only on the UI thread.
Instead, use an Executor for the background processing and call runOnUiThread when it is time to update the UI.
ExecutorService executorPool = Executors.newSingleThreadExecutor();
executorPool.execute(new Runnable() {
public void run() {
// do background processing here <------------------
myActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
// update ui here <--------------------
}
})
}
});

Android Timer update UI between multiple tasks

I have tried multiple ways to have a single persistent timer update the ui in multiple activities, and nothing seems to work. I have tried an AsyncTask, a Handler, and a CountDownTimer. The code below does not execute the first Log.i statement.... Is there a better way to start the timer (which must be called from another class) in Main (which is the only persistent class)?
public static void MainLawTimer()
{
MainActivity.lawTimer = new CountDownTimer(MainActivity.timeLeft, 1000)
{
public void onTick(long millisUntilFinished)
{
Log.i("aaa","Timer running. Time left: "+MainActivity.timeLeft);
MainActivity.timeLeft--;
if(MainActivity.timeLeft<=0)
{
//do stuff
}
else
{
//call method in another class
}
}
public void onFinish()
{ }
}.start();
}
To clarify my problem:
When I run the code the Log.i("aaa","Timer running") statement is never shown in the log, and the CountDownTimer never seems to start. MainLawTimer is called from another class only (not within the same class.
For CountDownTimer
http://developer.android.com/reference/android/os/CountDownTimer.html
You can use a Handler
Handler m_handler;
Runnable m_handlerTask ;
int timeleft=100;
m_handler = new Handler();
#Override
public void run() {
if(timeleft>=0)
{
// do stuff
Log.i("timeleft",""+timeleft);
timeleft--;
}
else
{
m_handler.removeCallbacks(m_handlerTask); // cancel run
}
m_handler.postDelayed(m_handlerTask, 1000);
}
};
m_handlerTask.run();
Timer
int timeleft=100;
Timer _t = new Timer();
_t.scheduleAtFixedRate( new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() //run on ui thread
{
public void run()
{
Log.i("timeleft",""+timeleft);
//update ui
}
});
if(timeleft>==0)
{
timeleft--;
}
else
{
_t.cancel();
}
}
}, 1000, 1000 );
You can use a AsyncTask or a Timer or a CountDownTimer.
Thank you all for your help, I discovered the error in my code... timeLeft was in seconds rather then milliseconds. Since timeLeft was under 1000 (the wait period) the timer never started.

How to call AyncTask with listener from a TimerTask in android?

I have to call a Async Task with listener from the TimerTask eack 1.5 seconds.When I tried to do that I'm getting an exception "java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare().". Please help us to solve this issue.
Following is the piece of code I use..
Timer t = new Timer();
TimerTask scanTask = new TimerTask()
{
#Override
public void run()
{
new BgTask((BgTaskListener)this).execute("","currentState");
}
};
t.schedule(scanTask, 1500, 1500);
Here BgTask id the AsyncTask class.
if you are in context of activity then this must work
runOnUiThread(new Runnable()
{
#Override
public void run()
{
new BgTask((BgTaskListener)this).execute("","currentState");
}
});

Android Timer thread which exits on a "is true" condition

I'm writing an Android application and I need a Timer to be set which will execute a method every one second and then stop once a boolean variable (set by aforementioned method) becomes true.
Here's an overview of what I'm trying to do:
boolean done = false;
public void someMethod() {
if(done == false) {
myTimer = new Timer(); //Set up a timer, to execute TimerMethod repeatedly
myTimer.schedule(new TimerTask() {
#Override
public void run() {
TimerMethod();
}
}, 0, 1000);
}
if(done == true) {
//TimerMethod will eventually set 'done' to true. When this happen, do layout modifying stuff here. Causes error as non-UI thread is executing the layout modifying stuff. Do I spawn a new UI thread here to do it? If so, how? :/
}
}
TimerMethod() {
String result = someServerMethod();
if(result == "theResultWeWant") {
myTimer.cancel(); //stop the timer - we're done!
done = true; //set 'done' to true so the line of code in someMethod() will now run
someMethod();
}
}
Edit: I've updated the code above to reflect what I'd like to do. I'm hoping I can get the done flash to be set to true and then carry on executing someMethod but I'm sure it's not that simple! Do I perhaps need to spawn a new thread from TimerMethod() to execute the code from the done == true line?
All UI interactions need to be done from the main(UI) thread. In your case, you were calling someMethod() from your TimerTask which is a seperate thread. A handler is used to interact with your main thread from a helper thread.
public void someMethod() {
myTimer = new Timer(); //Set up a timer, to execute TimerMethod repeatedly
myTimer.schedule(new TimerTask() {
#Override
public void run() {
TimerMethod();
}
}, 0, 1000);
}
}
TimerMethod() {
String result = someServerMethod();
if(result.equals("theResultWeWant")) {
myTimer.cancel(); //stop the timer - we're done!
mHandler.sendEmptyMessage(0): //send message to handler to update UI
}
}
private Handler mHandler = new Handler(){
public void handleMessage(Message msg) {
doUIMethod();
}
};

android timer handler

I am using this code:
public class newtimer extends Activity {
private Timer myTimer;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
myTimer = new Timer();
myTimer.schedule(new TimerTask() {
#Override
public void run() {
TimerMethod();
}
}, 0, 1000);
}
int number = 0;
private void TimerMethod()
{
//This method is called directly by the timer
//and runs in the same thread as the timer.
//We call the method that will work with the UI
//through the runOnUiThread method.
Toast.makeText(this, "TimerMethod Running "+number, Toast.LENGTH_SHORT).show();
this.runOnUiThread(Timer_Tick);
}
private Runnable Timer_Tick = new Runnable() {
public void run() {
//This method runs in the same thread as the UI.
//Do something to the UI thread here
Toast.makeText(getBaseContext(), "UI Thread Running "+number, Toast.LENGTH_SHORT).show();
}
};
}
When i run it i get this exception in logcat:
09-06 21:39:39.701: ERROR/AndroidRuntime(1433): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
I would assume the problem is with the Toast.makeText(this, "TimerMethod Running "+number, Toast.LENGTH_SHORT).show(); in your TimerMethod function - you can't call any functions pertaining to the UI from worker threads. Since you already have a Toast in the portion that runs in the UI thread, why do you have another one in TimerMethod?
For debugging, I would recommend using Log as much as possible instead of Toast.

Categories

Resources