Runnable Skipped Android AndEngine - android

I would like to say thanks first to you all..
I have made a code to generate sprite by using Runnable.
But it only executes the last runnable that I wrote.
Here's the code:
public class LevellingManager {
// ======================== //
// ======================== //
// THE PATH METHOD //
// ======================== //
// ======================== //
public static void path() {
currType = Leveling2.TYPE_DIGGER;
mHandler.post(mStartDigger);
// === NOT EXECUTED === //
generateSprite(Leveling2.TYPE_ROCK, 10, 2000, 2000, LevellingManager.EASY);
// === EXECUTED === //
generateSprite(Leveling2.TYPE_WORM, 8, 3000, 1000, LevellingManager.HARD);
}
The generateSprite() method
public static void generateSprite(int type, int nSprts, int delayPerSprt,
int delayPerBtch, int[] difficulty) {
currType = type;
switch (type) {
case Leveling2.TYPE_ROCK:
System.out.println("Awalnya : " + GameResources2.lastIndexRock);
// ---- Set Generate Data ---- //
currIndex = 1 + GameResources2.lastIndexRock;
currList= GameResources2.listRockToBeAdded;
currLastIndex = GameResources2.lastIndexRock;
currMaxCpcty= GameResources2.MAX_ROCK;
// --- Method to Set the position, speed & delay per sprite ---- //
set(nSprts, delayPerBtch, delayPerSprt, difficulty);
mHandler.postDelayed(mStartRock, currDelay);
GameResources2.lastIndexRock = currLastIndex;
break;
case Leveling2.TYPE_WORM:
// ---- Set Generate Data ---- //
currIndex = 1 + GameResources2.lastIndexWorm;
currList=GameResources2.listWormToBeAdded;
currLastIndex = GameResources2.lastIndexWorm;
currMaxCpcty = GameResources2.MAX_WORM;
// --- Method to Set the position, speed & delay per sprite ---- //
set(nSprts, delayPerBtch, delayPerSprite, difficulty);
mHandler.postDelayed(mStartWorm, currDelay);
GameResources2.lastIndexWorm = currLastIndex;
break;
}
}
Here comes the Runnable code:
public static Runnable mStartRock = new Runnable() {
public void run() {
installAttach(this);
}
};
public static Runnable mStartWorm = new Runnable() {
public void run() {
installAttach(this);
}
};
And here is a method that will be executed inside the Runnable.
installAttach() method:
private static void installAttach(Runnable r) {
System.out.println("Lewat sini, type : " + currType);
currSpriteGen = currList.get(currIndex);
currSpriteGen
.registerEntityModifier(new SequenceEntityModifier(
new MoveModifier(currSpeed,
currSpriteGen.getX(),
currSpriteGen.getX(),
GameResources2.CAMERA_HEIGHT,
GameResources2.POSITION_DIGGER_HEIGHT)));
currSpriteGen.setVisible(true);
if (currSpriteGen.getParent() == null)
GameResources2.scene.attachChild(currSpriteGen);
else
currLastIndex = currIndex;
switchLastIndex();
if (currIndex == currMaxCpcty - 1) {
currNSprts = currNSprts - currIndex;
currIndex = -1;
}
System.out.println("Index : " + currIndex);
System.out.println("Max index : " + currMaxCpcty);
++currIndex;
// --- For looping ---//
if (currIndex < currNSprts) {
mHandler.postDelayed(r, currDelay);
}
}
So when I call the LevellingManager.path() method at onLoadScene(),
only the last generate method that executed.
Please help me.
Thank you.

Apparently there is a place for only one Runnable. I believe I have a workaround for that. Create a class RunnableBatch whose instance will contain an array of Runnables. It will have a method add(Runnable r) that will
1) add the Runnable to the array and
2) create a new Runnable that will contain something like for (Runnable r : runnableArray) {r.run();}

Related

Load More RecyclerView Error Cannot Call this method

I need help for this load more RecycleView, I create 'Recyclerview', first, limit 5 data and showing, but when I scroll until the end no 'ProgressBar' showing and the next data now showing, I check the 'logcat' no error only warning
this my load more function
private void loadMore() {
arraylist.add(null);
mListadapter.notifyItemInserted(arraylist.size() - 1);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
arraylist.remove(arraylist.size() - 1);
int scrollPosition = arraylist.size();
mListadapter.notifyItemRemoved(scrollPosition);
int currentSize = scrollPosition;
int nextLimit = currentSize + 5;
Log.e("currentSize",""+currentSize);
Log.e("nextLimit",""+nextLimit);
if(nextLimit <= DataNoteImformation.id.length) {
while (currentSize + 1 <= nextLimit) {
DataNote wp2 = new DataNote(
DataNoteImformation.id[currentSize],
DataNoteImformation.branchArray[currentSize],
DataNoteImformation.assetcodeArray[currentSize],
DataNoteImformation.customerArray[currentSize],
DataNoteImformation.licenseplateArray[currentSize]
);
arraylist.add(wp2);
currentSize++;
}
}else {
while (currentSize + 1 <= DataNoteImformation.id.length) {
DataNote wp2 = new DataNote(
DataNoteImformation.id[currentSize],
DataNoteImformation.branchArray[currentSize],
DataNoteImformation.assetcodeArray[currentSize],
DataNoteImformation.customerArray[currentSize],
DataNoteImformation.licenseplateArray[currentSize]
);
arraylist.add(wp2);
currentSize++;
}
}
mListadapter.notifyDataSetChanged();
isLoading = false;
}
}, 2000);
}
and the warning in logcat
W/RecyclerView: Cannot call this method in a scroll callback. Scroll callbacks might be run during a measure & layout pass where you cannot change theRecyclerView data. Any method call that might change the structure of the RecyclerView or the adapter contents should be postponed to the next frame.
and this example data
public class DataNoteImformation {
public static String[] branchArray = {"Depok","Jakarta","Bekasi","Jakarta","Jakarta","Bekasi","Bogor","Jakarta","Bekasi","Jakarta"};
public static String[] assetcodeArray = {"0092","0084","0091","0084","0084","0078","0089","0073","0027","0021"};
public static String[] customerArray = {"Kevin Sanjaya","Indah Permata","Riyan","Puri Setiawan","Herman","Iwan","Ratna","Agus","Danang","Ujang"};
public static String[] licenseplateArray = {"B 9829 SK","B 8294 LK","B 9090 NBA","B 7627 SKA","B 7637 SAK","B 6763 KIK","F 7287 NB","F8792KI","B8273KD","B7728KSI"};
public static String[] id = {"1","2","3","4","5","6","7","8","9","10"};
}
how to fix this.

Trying to create an efficient infinite looping frame animation routine for Android

Since AnimationDrawable's memory blows up after a few images, I needed to write my own. Google has some demonstrations regarding efficient frame animations, but it is very complicated(needlessly?).
Here is my initial approach, can I get some experts/smart ppl to chime in?
Create a Handler on the UI Thread.
create runables (A) for the handler.
inside the runables (A), I create AsyncTasks for each frame.
each AsyncTask is run using PostDelay to achieve desired frame rate.
the target ImageVew is set wigh the new frame from the AsyncTask.
Here is the code:
private void addTask(Handler handler, final String animationName, final int frameNumber, final AsyncTaskLoadBitmap asyncLoadBitmap, int frameIndex) {
Runnable runnable = new Runnable() {
#Override
public void run() {
int frameIdentifier = getResources().getIdentifier(animationName, "drawable", getPackageName());
Log.v("Runable","adding name");
new AsyncTaskLoadBitmap(imageView,thisActivity).execute(frameIdentifier);
}
};
boolean done = handler.postDelayed(runnable,100 + (100 * frameIndex));
Log.v("PostDelayed","Done");
}
The problem I'm having is making it loop and a way to stopping the loop.
What could be a better approach?
try this:
final ImageView iv = new ImageView(this);
setContentView(iv);
final int LOAD = 1;
final int SHOW = 2;
HandlerThread loader = new HandlerThread("frameLoader");
loader.start();
Callback callback = new Callback() {
int idx = 0;
int[] ids = {
R.drawable.ic_launcher, R.drawable.layer0, R.drawable.layer1,
};
Resources res = getResources();
#Override
public boolean handleMessage(Message msg) {
if (msg.what == LOAD) {
Log.d(TAG, "handleMessage LOAD " + Thread.currentThread().getName());
idx++;
if (idx < 18) {
Bitmap btm = BitmapFactory.decodeResource(res, ids[idx % ids.length]);
msg = mUIHandler.obtainMessage(SHOW, btm);
mUIHandler.sendMessage(msg);
}
} else
if (msg.what == SHOW) {
Log.d(TAG, "handleMessage SHOW " + Thread.currentThread().getName());
iv.setImageBitmap((Bitmap) msg.obj);
mBackgroundHandler.sendEmptyMessageDelayed(LOAD, 500);
}
return true;
}
};
mBackgroundHandler = new Handler(loader.getLooper(), callback);
mUIHandler = new Handler(callback);
mBackgroundHandler.sendEmptyMessage(LOAD);

java.lang.IndexOutOfBoundsException... But why?

After 3 hours of try i decided to ask here and see if someone can provide me a solution for this error: java.lang.IndexOutOfBoundsException: Invalid index 4, size is 4
Here is my code.
private ProgressBar progressBar;
private int progressStatus = 0;
private Handler handler = new Handler();
--------------------
status.setText(getString(R.string.init));
progressBar.setVisibility(View.VISIBLE);
final Map<String,?> keys = prefs.getAll(); //4 prefs atm inside.
final ArrayList<String> props = new ArrayList<String>();
final ArrayList<String> values = new ArrayList<String>();
if (keys != null) {
for(final Map.Entry<String,?> entry : keys.entrySet()) {
props.add(entry.getKey());
values.add(entry.getValue().toString());
}
final int total = props.size();
progressBar.setMax(total);
new Thread(new Runnable() {
public void run() {
while (progressStatus < total) {
progressStatus += 1;
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
if (props.get(progressStatus) != null) {
writeProps(props.get(progressStatus), values.get(progressStatus));
status.setText(getString(R.string.writing) + ": " + props.get(progressStatus));
}
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (progressStatus == total) {
handler.post(new Runnable() {
public void run() {
progressBar.setVisibility(View.GONE);
progressStatus = total + 1;
myVoidOnFinish();
}
});
}
}
}).start();
}
I get the stuck here:
writeProps(props.get(progressStatus), values.get(progressStatus));
status.setText(getString(R.string.writing) + ": " + props.get(progressStatus));
The main problem is here
while (progressStatus < total) {
progressStatus += 1;
}
check value of your progress status it should be not greater than 3 while in you code it is 4 in last while index is only from 0 to 3. please set value of your progress status accordingly.
try this
while (progressStatus < total-1) {
progressStatus += 1;
}
The problem is you are setting the progressstatus value like following
final int total = props.size();
progressBar.setMax(total);
and you in the following line you are trying to get values of props ase
writeProps(props.get(progressStatus), values.get(progressStatus));
see that for the total size of 4 your progressStatus will be 4. But the maximum index of props and values will be 3. So here is the problem
you can set the value to 1 less than the size like
final int total = props.size() - 1;
You are using one extra index as you can use maximum up to 3. So please start it form zero and use less then check instead of less then equal to.

Thread "run twice" in android when resume

I have really wired problem..I searced a lot in google and here and con't solve my problem.
I have main activity which create and start 3 threads (only "gameThread" is relevant now).
When I click on the back buttom it seems ok , but when I come back to the activity and the thread continue running , it seems it run duplicate - I see only one ContentView on the screen, but there is more one running (maybe behind).
I hope I explain myselg good..
this is the code:
package com.example.newpingpong;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.Toast;
/*
* The main activity class of the Ping Pong game
*/
public class MainActivity extends Activity
{
/*
* The following class variables hold references to the objects the game is composed of -
* PingPongView - main view,
* gameBall - the ball in the game,
* gamePaddle - the paddle in the game,
* gameThread - main thread updating the position of the ball,
* paddleMoverThread - another thread which is responsible for moving the paddle.
*/
private PingPongView gameView;
private Tray gamePaddle;
private GameLevel gameLevel;
private GameLevel [] levels;
//threads
private PingPongGame gameThread;
private PaddleMover paddleMoverThread;
private PresentThread giftThread;
private Messages message;
//balls container
private BallView [] ballViewArr;
private Ball [] ballArr;
//gifts container
private Gift[] giftArr;
private GiftView[] GiftViewArr;
public GameSounds gameSounds;
private int screenWidth;
private int screenHeight;
private TrayView paddleView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
Toast.makeText(this, "onCreate!", Toast.LENGTH_SHORT).show();
// Get the message from the intent
Intent intent = getIntent();
String levelChosed = intent.getStringExtra(ImageAdapter.EXTRA_MESSAGE);
int levelChosed_int = Integer.parseInt(levelChosed);
Const.currentLevel = levelChosed_int;
Const.isCompleteThisLevel = false;
GameLevelView gameLevelView;
//for sounds
if(gameSounds == null){
gameSounds = new GameSounds();
gameSounds.addSound(Const.CATCH_GIFT, R.raw.cathc_gift, getBaseContext());
gameSounds.addSound(Const.GIFT_START_FALL, R.raw.gift_start_fall, getBaseContext());
gameSounds.addSound(Const.HIT_BLOCK, R.raw.hit_block, getBaseContext());
gameSounds.addSound(Const.HIT_KIR, R.raw.hit_kir, getBaseContext());
gameSounds.addSound(Const.GAME_OVER, R.raw.game_over, getBaseContext());
gameSounds.addSound(Const.HIT_PADDLE, R.raw.hit_paddle, getBaseContext());
}
if(this.ballViewArr == null){
this.ballViewArr = new BallView[Const.MAX_BALLS];
this.ballArr = new Ball[Const.MAX_BALLS];
this.giftArr = new Gift[Const.MAX_GIFTS];
this.GiftViewArr = new GiftView[Const.MAX_GIFTS];;
// Getting the screen width & height
screenWidth = this.getWindowManager().getDefaultDisplay().getWidth();
screenHeight = this.getWindowManager().getDefaultDisplay().getHeight();
for(int i=0 ; i< Const.MAX_BALLS ; i++){
this.ballArr[i] = new Ball(screenWidth / 2, screenHeight / 2, screenWidth, screenHeight);
this.ballViewArr[i] = new BallView(this, this.ballArr[i]);
this.ballArr[i].setMode(Const.NOT_ACTIVE);
}
this.ballArr[0].setMode(Const.ACTIVE);
for(int i=0 ; i< Const.MAX_GIFTS ; i++){
this.giftArr[i] = new Gift(screenWidth / 2, screenHeight / 4,screenWidth, null, Const.NOT_ACTIVE);
this.GiftViewArr[i] = new GiftView(this, this.giftArr[i]);
}
// Creating the paddle
gamePaddle = new Tray(screenWidth / 2, (int)(screenHeight * 0.75), screenWidth);
// Creating the paddle view, and giving it the gameBall as a parameter
paddleView = new TrayView(this, gamePaddle);
levels = new GameLevel[Const.LevelsAmount];
levels[0] = new GameLevel(Const.level0, screenWidth , screenHeight, this.giftArr);
levels[1] = new GameLevel(Const.level1, screenWidth , screenHeight, this.giftArr);
levels[2] = new GameLevel(Const.level2,screenWidth , screenHeight, this.giftArr);
}
gameLevel=levels[levelChosed_int];
gameLevelView = new GameLevelView(this,gameLevel);
// Creating the game view
this.gameView = new PingPongView(this);
message = new Messages(this , screenWidth , screenHeight);
// Give the gameView our ballView & paddleView.
gameView.setViews(paddleView,gameLevelView , message ,this.ballViewArr , this.GiftViewArr);
// Setting the gameView as the main view for the PingPong activity.
setContentView(gameView);
if(gameThread == null){
//create the main thread
gameThread = new PingPongGame(this.getResources(), gamePaddle, gameView, gameLevel , message , ballArr , gameSounds);
//create the thread responsible for moving the paddle
paddleMoverThread = new PaddleMover(gamePaddle, gameView);
//create the thread responsible for present
giftThread = new PresentThread(gamePaddle , gameView , gameLevel, message , giftArr , ballArr,gameSounds );
gameThread.start();
paddleMoverThread.start();
giftThread.start();
}
}
//This method is automatically called when the user touches the screen
#Override
public boolean onTouchEvent(MotionEvent event)
{
float destination;
// Toast.makeText(this, "try!", Toast.LENGTH_SHORT).show();
//get the x coordinate of users' press
destination = event.getX();
//notify the paddle mover thread regarding the new destination
gamePaddle.setPaddleDestination(destination);
return true;
}
#Override
protected void onPause() {
Toast.makeText(this, "onPause!", Toast.LENGTH_SHORT).show();
Const.toStop = true;
while(Const.toStopMessageHasRecieved == false){
} //the thread not see the message that he shoult stop
//here the thrad recived the message and stoped.
super.onPause();
}
#Override
public void onResume()
{
Toast.makeText(this, "onResume!", Toast.LENGTH_SHORT).show();
for(int i=0 ; i< Const.MAX_BALLS ; i++){
this.ballArr[i] = new Ball(screenWidth / 2, screenHeight / 2, screenWidth, screenHeight);
this.ballViewArr[i] = new BallView(this, this.ballArr[i]);
this.ballArr[i].setMode(Const.NOT_ACTIVE);
}
this.ballArr[0].setMode(Const.ACTIVE);
synchronized (Const.mPauseLock) {
Const.toStop = false;
Const.mPauseLock.notifyAll();
}
super.onResume();
}
}
AND THE THREAD:
package com.example.newpingpong;
import android.content.res.Resources;
import android.graphics.Color;
public class PingPongGame extends Thread {
private PingPongView gameView;
private Tray gamePaddle;
private GameLevel gameLevel;
private Messages message;
private Ball [] ballArr;
private GameSounds gameSounds;
private Resources res;
//private int screenWidth;
//private int screenHight;
public PingPongGame(Resources res ,Tray theTray , PingPongView mainView , GameLevel gameLevel ,
Messages message , Ball ballArr[] ,GameSounds gameSounds)
{
this.gameView = mainView;
this.ballArr = ballArr;
this.gamePaddle = theTray;
this.gameLevel=gameLevel;
this.message = message;
this.gameSounds=gameSounds;
this.res=res;
//this.screenWidth = screenWidth;
//this.screenHight = screenHight;
//for stop and resume threads
}
//main method of the current thread
#Override
public void run()
{
try {
PingPongGame.sleep(2000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
this.message.setMessage(res.getString(R.string.lives) + gameLevel.getLives());
gameView.postInvalidate();
//infinitely loop, and move the balls accordingly
while ((Const.isLose == false) && (Const.isCompleteThisLevel==false))
{
for(int i=0; i<Const.MAX_BALLS ; i++){
if(ballArr[i].getMode() != Const.NOT_ACTIVE){
//move the ball one step
if(ballArr[i].move()) //hit kir
gameSounds.play(Const.HIT_KIR,false);
gameView.postInvalidate();
//check if the ball hit some block
if(gameLevel.updateIfHit(ballArr[i])){
ballArr[i].down();
gameSounds.play(Const.HIT_BLOCK,false);
}
gameView.postInvalidate();
if(gameLevel.getLives() == 0){
//gameLevel.restart();
this.message.setMessage(res.getString(R.string.game_over));
gameView.postInvalidate();
gameSounds.play(Const.GAME_OVER,false);
Const.isLose = true;
try
{
PingPongGame.sleep(2000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
//check if the ball has reached the paddle
if(checkHitPaddle(i,ballArr[i]) == true){ //משתמש פספס and lives --
this.message.setMessage(res.getString(R.string.lives)+"" + gameLevel.getLives());
//send a request to refresh the display
gameView.postInvalidate();
}
}
}
try
{
PingPongGame.sleep(5);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//public static boolean toStop = false;
//public static boolean toStopMessageHasRecieved = false;
//for stop and resume
//for stop and resume
synchronized (Const.mPauseLock) {
while (Const.toStop) {
Const.toStopMessageHasRecieved = true;
try {
Const.mPauseLock.wait();
} catch (InterruptedException e) {
}
}
}
}
// Const.toStopMessageHasRecieved = true;
}
//This method is responsible for handling the situation in which the ball has reached the height of the paddle
//return true if lives--
private boolean checkHitPaddle(int i , Ball ball)
{
// Checking if the ball had hit the paddle
if (ball.getBottom() >= gamePaddle.getTop())
{
// check if the paddle is in the right place
if ((ball.getRight() > gamePaddle.getLeft()) &&
(ball.getLeft() < gamePaddle.getRight()))
{
//The ball had hit the paddle - so it should be bounced
gameSounds.play(Const.HIT_PADDLE,false);
ball.bounceUp();
}
else
{
//The ball had missed the paddle - player looses - restart the game
gameSounds.play(Const.GAME_OVER,false); //not really game over - just lose live
boolean isMoreBall = false;
for(int j=0 ; j< Const.MAX_BALLS ; j++)
if( (j != i) && (ballArr[j].getMode() == Const.ACTIVE)){
isMoreBall = true;
break;
}
if(isMoreBall){
ball.setMode(Const.NOT_ACTIVE);
//for(int k=0; k<Const.MAX_BALLS ; k++){
//ballArr[k].downSpeed(0.25);
//}
}
else{
gameLevel.setLives(gameLevel.getLives()-1);
ball.restart();
return true;
}
}
}
return false;
}
}
Everytime the Activity is started you create a new gameThread in onCreate since the Activity is a new object then and gameThread is null. But you never stop them. You should probably insert something that ends your threads in onDestroy.
At the moment both Threads modify your Const object. Which leads me to the main problem:
I think the main problem is that all your communication is handled by some 'Global Variable Collection' in the Const object which can be very confusing the bigger the game gets. For now a solution could be to let the Const object handle the Threads too, but that would possibly only make it worse on the long run.
Try using
Timer rather than Thread.
And cancel timer using timer.cancel() in onBackPressed() method.

How to stop Executors.newSingleThreadScheduledExecutor(); in between the thread execution?

Below I am posting my code for the thread I am running to animate text in a RelativeLayout on top of the Page Curl activity by harism.
public void startProgress(final int index)
{
runnable = new Runnable()
{
#Override
public void run()
{
mArrWords.removeAll(mArrWords);
mStart.removeAll(mStart);
mEnd.removeAll(mEnd);
words = sentence.split(" ");
for(int i = 0; i < words.length; i++)
{
mArrWords.add(words[i]);
if(i == 0)
{
mStart.add(0);
mEnd.add(words[0].length());
}
else
{
mStart.add(mEnd.get(i-1)+1);
mEnd.add(mStart.get(i)+words[i].length());
}
/*Log.e("words", "" + "" + words[i]);
Log.e("mArrWords", "" + mArrWords);
Log.e("mStart", "" + mStart);
Log.e("mEnd", "" + mEnd);*/
}
for (int i = 0; i < mArrWords.size(); i++)
{
final int value = i;
try
{
Thread.sleep(500);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
mHandler.post(new Runnable()
{
#Override
public void run()
{
currIndex = index;
try
{
if(CurlView.ANIMATE)
tv1.setVisibility(View.VISIBLE);
else
tv1.setVisibility(View.GONE);
final Pattern p = Pattern.compile(mArrWords.get(value));
final Matcher matcher = p.matcher(sentence);
SpannableString spannableTxt = new SpannableString(sentence);
ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
while(matcher.find())
spannableTxt.setSpan(span, mStart.get(value), mEnd.get(value), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv1.setText(spannableTxt);
mHandler.sendEmptyMessage(0);
}
catch (Exception e)
{
e.printStackTrace();
mHandler.sendEmptyMessage(0);
}
}
});
}
}
};
final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor();
worker.schedule(runnable, CurlView.ANIMTIME+50, TimeUnit.MILLISECONDS);
}
Here, I am animating the text over images. I need to change the text for each page I am changing. I am able to change the text, however, when I turn the page, the index values I store in ArrayLists are not getting cleared. I am storing a sentence in an ArrayList named mArrWords and the indexes to refer to each word of sentence are stored in mStart and mEnd.
The problem I am facing is when the text changes, the animation starts with the previous indexes stored in mStart and mEnd ArrayLists I use to store index of a particular word. What I need to know is how do I stop my thread when the page is turned or the index of the page changes. I am calling this function inside the updatePage(final CurlPage page, final int width, final int height, final int index) method of Curl activity. I hope I was able to explain my problem. Thanks!
EDIT: I would like to specify my question more clearly. How do I check if the thread is already running before starting another thread and stop the execution of the previous thread?
removeCallbacks(..) only stops pending messages (Runnables).If runnable is started then u can not stop it in this way. See the following :
removecallbacks-not-stopping-runnable

Categories

Resources