How to use async task to control a view pager - android

I am trying to create an auto image slider using a view pager following some tutorials. I got everything working, but then I see
Choreographer: Skipped 1 frames! The application may be doing too much work on its main thread. and googled the error.`
You see, in my code, I have a timer and a handler that just delay the code for 3 seconds and then slides the view pager to the next image. I confirmed with a friend and after hours of searching, there was nothing else that was doing too much work on the main thread.
So searching on stack overflow about the problem, I see that a lot of developers suggested using AsyncTask to do some stuff in background and then update, which might actually be a perfect solution here. But then I realised that I know nothing about AsyncTask. I went to the android developers reference and saw some tutorials, but I was unable to find something that'll fit into my solution.
I saw a lot of tutorials about image downloading, but they are by far, not what I'm concerned with. I am only concerned with controlling the view pager to move to the next slide.
Here is the part where I control my view pager to slide:
// Auto start of viewpager
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (currentPage == NUM_PAGES) {
currentPage = 0;
}
mPager.setCurrentItem(currentPage++, true);
}
};
Timer swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
#Override
public void run() {
handler.post(Update);
}
}, 3000, 3000);
How do I incorporate this code into an AsyncTask?
For reference, I used this tutorial.

Runnable is a seprate worker thread, it has no interaction with main thread. When you are handling ui elements, use runOnUiThread. example.
runOnUiThread(new Runnable() {
#Override
public void run() {
if (currentPage == NUM_PAGES) {
currentPage = 0;
}
mPager.setCurrentItem(currentPage++, true);
}
});
or
runOnUiThread(){
mPager.setCurrentItem(currentPage++, true);
}

Below you can find two methods on how can one run a code on the main/UI thread on android:
runOnUIThread Activity’s method
runOnUiThread(new Runnable(){
public void run() {
// UI code goes here
}
});
Handler
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
public void run() {
// UI code goes here
}
});
source

Related

lagging with animation, handler, thread in android studio

I want to make a custom menu bar with slidingPanel activity.
But there was lagging when I start animation.
I searched and figured out there is UI thread and recommended to use
thread and handler. I rewrite code but there is still lagging when I active
below function.
Is there any wrong?
public void animHandler(){
final Handler mHandler = new Handler();
Thread t = new Thread(new Runnable() {
#Override
public void run() {
mHandler.post(new Runnable() {
#Override
public void run() {
slidingPanel = (RelativeLayout) findViewById(R.id.slidingRel);
slidingPanel.startAnimation(translateLeftAnim);
slidingPanel.setVisibility(View.VISIBLE);
}
});
}
});
t.start();
}
thanks for reading!
Having it executed on a different thread but then updating it in the UI thread won't exactly fix the lagging issues, since it is still executed in the main thread.
You could either use SurfaceView to update all of the UI on a background thread or just completely make it a custom view which properly handles the animation.

runOnUiThread() no executing when using Thread.sleep()

I'm using code that looks like this :
_thread = new Thread(){
#Override
public void run() {
try {
while (true) {
operate();
Thread.sleep(DELAY);
}
} catch (InterruptedException e) {
// Doesn't matters...
}
}
};
operate function looks like this :
// does things....
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
// adds an ImageView to the screen
}
});
// does other things...
At the bottom line, what i wanted to achieve is an operation that happens once in a while, without interrupting the main thread and the UI, something like a game-loop.
In the first 2 times that operate() runs, it adds the ImageView and everything is alright, but after 2 or 3 times it stops adding the ImageViews, but the UI is still running as usual. When i debugged the problem, i found out that after 3 times the run() method of the Runnable isn't called anymore, even thought the operate function was called.
The wired thing (for me) was that when i removed the Thread.sleep, everything worked fine (much faster of course...). I tried to replace it with a very long for loop (just for checking) and it worked, but of course it is not an appropriate solution to the problem.
I read about the problem, most of the people that asked this question did a thread.sleep or an infinite loop on the main thread, but, as i see it, i didn't do such thing. Many people wrote that you should replace the Thread.sleep with Handler.postDelayed. I tried to do it but it didn't work, maybe I did it wrong. I even tried replacing the runOnUiThread with other options I found on the internet, but all of them gave me the same exact results. I tried to replace the method that I'm adding the view to the activity, but all of them, again, gave the same result.
The waiting is crucial for this application. I got to find a way to wait sometime and then execute a function on the UI thread, cause this pattern returns at least a couple of times in my application.
It sounds like you want a post delay so that you can do the code on the UI thread after some delay. Handler Post Delay.
private static final int DELAY = 500;
private Handler mHandler;
private Runnable mRunnable;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start();
}
private void start()
{
mHandler = new Handler();
mRunnable = new MyRunnable(this);
mHandler.postDelayed(mRunnable, DELAY);
}
private void stop()
{
mHandler.removeCallbacks(mRunnable);
}
private void doSomething()
{
// Do your stuff here.
// Reschedule.
mHandler.postDelayed(mRunnable, DELAY);
}
Recommended way of creating a Runnable.
private static class MyRunnable implements Runnable
{
private WeakReference<MainActivity> mRef;
// In here you can pass any object that you need.
MyRunnable(MainActivity activity)
{
mRef = new WeakReference<MainActivity>(activity);
}
#Override
public void run()
{
// Safety check to avoid leaking.
MainActivity activity = mRef.get();
if(activity == null)
{
return;
}
// Do something here.
activity.doSomething();
}
}
There could be several reasons why the UI Runnable isn't being executed. Probably the activity variable has something messed up with it or it's referencing the context incorrectly, or as you said the Thread.sleep() could be causing an issue. At this point more parts of the code needs to viewed to better solve the problem.
A better way of implementing your logic is to use a scheduled Timer instead of using an infinite loop with a Thread.sleep() in it. It will execute the code within a background thread. And then use a Handler to update the UI instead of activity.runOnUiThread(). Here's an example:
// Global variable within the activity
private Handler handler;
// Activity's onCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
handler = new Handler(getMainLooper());
Timer timer = new Timer("ScheduledTask");
// Timer must be started on the Main UI thread as such.
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
operate();
}
}, 0L, DELAY);
}
private void operate() {
// does things in background....
handler.post(new Runnable() {
#Override
public void run() {
// adds an ImageView to the screen from within the Main UI thread
}
});
// does other things in the background...
}

android handler - ok, timer - unfortunately stopped. why difference?

I'm using Android Studio and emulator android 4.1.2.
My code
Timer timer = new Timer ();
timer.schedule(new TimerTask() {
#Override
public void run() {
myRun();
}
},10000,10000);
result in "unfortunately, app has stopped", however I found out the code
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
myRun();
}
}, 2000);
runs ok and displays as I expect.
What is the inner difference between the two?
P.S.
public void myRun () {
myView.removeAllViews();
drawView = new DrawView(myContext, myView);
myView.addView(drawView);
}
I'm trying to perpetually update a custom view until user cancels it. Just cycle
while (myRun) {
results in emulator becoming unresponsive to even back button, running that update in second thread
new Thread(new Runnable() {
#Override
public void run() {
while (myRun) {
myView.post(new Runnable() {
public void run() {
myView.removeAllViews();
drawView = new DrawView(myContext, myView);
myView.addView(drawView);
}
});
}
}
}).start();
results in same (interestingly to me, if I run debug with breakpoint on while in second thread, screen updates as I expect many times, however running w/out debugging does not update screen).
Timer executes its tasks on a separate thread that is used only for serving tasks created by this particular timer. Handler runs its task on its Looper's thread which may or may not be a UI thread. Generally speaking there's no much difference between this two classes if you use Handler on a separate thread. But it's more common in Android to use Handler and HandlerThread.
If you need to interact with UI, you'd better use Handler.
This is an interesting question and answer lies in Thread/GUI policy that android follows.
As we know, UI runs on main thread. Timer creates a different thread and android does not allow to update UI in a different thread. Why?
Suppose, you have started a thread in your activity that updates a TextView and while the thread is running you move to some other app. Now, main thread no longer exists and when the other thread tries to update the TextView it is not able to find that TextView. As a result, we see a crash.
Now let me come to the difference between TimerTask and Handler.
TimerTask creates a new thread, waits for the time specified and then executes run() method in the same thread. On the other hand, Handler creates a new thread, waits for specified duration then returns to main thread and executes run() method on MAIN thread(if handler is on main thread). Hence, it works fine.
However you can do it with timer too.
See the code below:
final Runnable setRunnable = new Runnable() {
public void run() {
myView.removeAllViews();
drawView = new DrawView(myContext, myView);
myView.addView(drawView);
}
};
TimerTask task = new TimerTask(){
public void run() {
getActivity().runOnUiThread(setRunnable);
}
};
Timer timer = new Timer();
timer.schedule(task, 1000);
In this thread you are setting a runnable to run on UI thread after timer's duration.

How to delay execution android

I am building an android board game which features AI. The AI gets a turn and has to invoke a series of actions after which it posts invalidate to my custom view to update.
I need to slow down these actions so the user gets to see the AI having its turn rather than it flashing by.
I have tried something along these lines
try {
doFirstThing();
Thread.sleep(500)
//post invalidate
doNextThing();
Thread.sleep(1000)
//post invalidate
}
catch (Exception e) {
}
However this is having absolutely no effect. Also this is running in a separate thread if this wasn't obvious.
Whats my best option I've looked at handler but they don't need right as i need to execute a series of tasks in sequence updating the view each time.
Using a Handler, which is a good idea if you are executing from a UI thread...
final Handler h = new Handler();
final Runnable r2 = new Runnable() {
#Override
public void run() {
// do second thing
}
};
Runnable r1 = new Runnable() {
#Override
public void run() {
// do first thing
h.postDelayed(r2, 10000); // 10 second delay
}
};
h.postDelayed(r1, 5000); // 5 second delay
Just to add a sample :
The following code can be executed outside of the UI thread.
Definitely, Handler must be use to delay task in Android
Handler handler = new Handler(Looper.getMainLooper());
final Runnable r = new Runnable() {
public void run() {
//do your stuff here after DELAY milliseconds
}
};
handler.postDelayed(r, DELAY);

Android Async, Handler or Timer?

Every 5 seconds, I want to call my webservice and get text (not images), then display it in my ImageAdapter. What would be the best way to accomplish this?
final Handler handler = new Handler();
final Runnable r = new Runnable()
{
public void run()
{
callWebservice();
}
};
handler.postDelayed(r, 5000);
It depends if you want to use a different thread or not. Do you want the user to be able to interact with the application on the UI Thread while the images are downloading? If so, then I would definitely use an AsyncTask with a small ProgressBar (style="#android:style/Widget.ProgressBar.Small")
If you don't care about threading then what #inazaruk said.
Edit: the truth is most modern apps that retrieve data from a web service will use an AsyncTask with a discreet little loader in the corner just to let the user know it's updating.
Edit 2: here's an example of using a TimerTask to run something every 5 seconds. The key is the runOnUiThread(). There may be better ways to tie all the elements together but this accurately portrays all the pieces.
myTimer = new Timer();
myTimer.schedule(new TimerTask() {
#Override
public void run() {
CallWebService();
}
}, 0, 1000);
}
private void CallWebService()
{
this.runOnUiThread(fetchData);
}
private Runnable fetchData = new Runnable() {
public void run() {
asyncTask.execute();
}
};
You should call asynctask inside the application main thread. Asynctask can't be called in a background thread.

Categories

Resources