0
   oncreate(){
setUpDemo();
}

  public void setupDemo(){

            imageDemo=new ImageView(this);

            RelativeLayout.LayoutParams rLayParamsDirectiveBtnImg = new RelativeLayout.LayoutParams(height/9,height/9);

            rLayout= (RelativeLayout) findViewById(R.id.relative_layout);
            rLayout.setBackgroundColor(Color.MAGENTA);

            demoObjectObject = dbHandler.getDemoObjectObject(trainingID);

            final RelativeLayout.LayoutParams rLayParams = new RelativeLayout.LayoutParams(height/3,height/3);

            //putting middle to show demo of items in each iteration
            rLayParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
            rLayParams.addRule(RelativeLayout.CENTER_IN_PARENT);

            handler = new Handler();

            int i=0;

           while (  i <demoObjectObject.size() ) {
               // Log.d(TAG, "for i="+ i);

               speech = "";
               final int finalI = i;
               handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        ObjectObject demoObject = new ObjectObject();
                        demoObject = demoObjectObject.get(finalI);

                        speech = KEY_THIS;


                            speech = speech + KEY_READ[1]+ dbHandler.getColorName(demoObject.getColorID());


                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            tts.speak(speech, TextToSpeech.QUEUE_FLUSH, null, null);
                        }
                        else{
                            tts.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
                        }

                        imgBytes = demoObject.getObjectImageBlob();
                        bmp = BitmapFactory.decodeByteArray(imgBytes, 0, imgBytes.length);

                        rLayout.removeView(imageDemo);
                        imageDemo.setImageBitmap(bmp);

                        rLayout.addView(imageDemo,rLayParams);

                    }
                }, 5000 * i);

               i++;
                       }//while end

            handler.postDelayed(new Runnable() {

                @Override
                public void run() {

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        tts.speak("now it is starting...", TextToSpeech.QUEUE_FLUSH, null, null);
                    }
                    else{
                        tts.speak("now startnig...", TextToSpeech.QUEUE_FLUSH, null);
                    }

                }
            }, 5000 * demoObjectObject.size());

            handler.postDelayed(new Runnable() {

                @Override
                public void run() {
                    setupForGame();
                }
            }, 5000 * demoObjectObject.size()+6000);


        }

As you can see, I have a handler. Inside, there are 3 runnables in a queue. First one is in a while loop. At each iteration the handler waits for 5 seconds.

When I stop the activity ( going home or desktop for example), I want to freeze this wait time with stopping tts. When I come back, it should continue to speak from the beginning and maybe it should start from the first second of wait ( for example I left at 3 seconds but it can start from 0 second).

I try those

@Override
    protected void onResume() {

        super.onResume();
        if (handler != null) {
            synchronized (handler) {//this doesnot work.

//speak again
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    tts.speak(speech, TextToSpeech.QUEUE_FLUSH, null, null);
                }
                else{
                    tts.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
                }
            }
        }
    }

@Override
    protected void onStop() {
        Log.v(TAG, "choosing gameonstop");
        super.onStop();
       handler.removeCallbacksAndMessages();//this needs paramter but i dont have to put inside?
    }

    @Override
    protected void onPause() {

        super.onPause();

    }
gcourtet
  • 74
  • 2
  • 10
  • 2
    don't enqueue everything at once, you can't pause / stop / resume a handler, you can only remove (http://stackoverflow.com/questions/5883635/how-to-remove-all-callback-from-a-handler) runnables and re-enqueue them with the remaining delay time. – zapl May 30 '16 at 10:02
  • I made all runnables with names r1 r2 r3, but how will i understand in which runnable it is stoıpping because i need to know where to go on when onresume. Should i use three booleans for that, boolean r1completed=false only way is that? and you said remaining delay time, i couldnot see there in your link? –  May 30 '16 at 10:33

1 Answers1

0
public class YourActivity extends AppCompatActivity {

    private static boolean handlerflag=false;
    private Handler handler;
    private Runnable runnable;
    private int myind=0,index=0,count=0;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_activtiy);         
        //oncreate exe only
        handlerflag=true;
        handler = new Handler();
        startyourtime(0);
 }
  private void  startyourtime(int a) {

    myind=0;
    for (index=a; index<10 ;index++) {
            myind++;
            runnable=new Runnable() {
                count++;
                @Override
                public void run() {
                          //your code here
               }
            };handler.postDelayed(runnable, Constants.TIME_LIMIT * myind);

   }
    @Override
    protected void onPause() {
        super.onPause();
        handlerflag=false;
        handler.removeCallbacksAndMessages(null);
    }
    @Override
    protected void onResume() {
        super.onResume();
        if(!handlerflag)
        {
           startyourtime(count);

        }
    }
    @Override
    protected void onDestroy() {
      super.onDestroy();
      handler.removeCallbacksAndMessages(null);
    }
}