My handlers don't work smooth.How can i fix it?












0















Hello everyone i try to get commands from txt file and I want to run these commands in order for a certain period of time.I can get the commands from the txt file correctly but when I want to do these commands using handlers for a certain period of time but there is a problem.The problem is handlers work together,the other Handler comes into play before the first Handler's time expires.When i did that with same runnable it did same thing.



My txt file looks like:
Ileri,5,1 /n
Sol,10,1 /n -->10 is operation time 1 is The number of repetitions.
Sag,5,1 /n
Geri,10,1 /n



My output:



I/System.out: Ileri
6000
1
I/System.out: Sol
11000
1
I/System.out: Sag
6000
1
I/System.out: Geri
11000
1
I/System.out: A



    start_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer=new Timer();

String yon;
String saniye1;
String tekrar;
int i=0,j=0,c=0,d=0;

ArrayList<String> listS=new ArrayList<String>();

try {
Scanner s=new Scanner(new File("/data/data/com.example.emrecan.myapplication/files/komutlar.txt"));

while(s.hasNextLine())
{
listS.add(s.nextLine());
}

} catch (FileNotFoundException e) {
e.printStackTrace();
}

String line2=new String[100];
for(String str:listS)
{
String line=str.split(",");
line2[c]=line[i];
line2[c+1]=line[i+1];
line2[c+2]=line[i+2];
c=c+3;
}
//String gonderilecek=new String();
String line3=new String[c+2];
while(d<c)
{
line3[d]=line2[d];
d=d+1;
}
d=0;
tekrar=line3[2];
int tekrar1=Integer.parseInt(tekrar);



while(d<tekrar1)
{

while(j<=c-2)
{

yon = line3[j];
saniye1 = line3[j + 1];
sure1 = Long.parseLong(saniye1);
sure1=sure1*1000;
sure1=sure1+1000;
System.out.println(yon);
System.out.println(sure1);
System.out.println(tekrar1);
e=0;
switch (yon)
{
case "Ileri":

runnable=new Runnable() {
@Override
public void run() {
sure1=sure1-1000;
if(sure1>0)
{
handler.postDelayed(this,1000);
mp.start();
}
else
{
handler.removeCallbacks(runnable);
}
}
};

handler.postDelayed(runnable,1000);
Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();

break;


case "Sol":

runnable2=new Runnable() {
@Override
public void run() {
sure1=sure1-1000;
if(sure1>0)
{
handler.postDelayed(this,1000);
mp2.start();
}
else
{
handler.removeCallbacks(runnable2);
}
}
};
handler.postDelayed(runnable2,1000);
Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
break;


case "Sag":


runnable3=new Runnable() {
@Override
public void run() {
sure1=sure1-1000;
if(sure1>0)
{
handler.postDelayed(this,1000);
mp3.start();
}
else
{
handler.removeCallbacks(runnable3);
}
}
};
handler.postDelayed(runnable3,1000);

Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
break;


case "Geri":

runnable4=new Runnable() {
@Override
public void run() {
sure1=sure1-1000;
if(sure1>0)
{
handler.postDelayed(this,1000);
mp4.start();
}
else
{
handler.removeCallbacks(runnable4);
}
}
};
handler.postDelayed(runnable4,1000);

Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
break;

default:
Toast.makeText(getApplicationContext(), "Sıkıntı var!", Toast.LENGTH_LONG).show();
break;

}


j=j+3;
}
j=0;
d=d+1;
System.out.println('A');
}

}
});









share|improve this question



























    0















    Hello everyone i try to get commands from txt file and I want to run these commands in order for a certain period of time.I can get the commands from the txt file correctly but when I want to do these commands using handlers for a certain period of time but there is a problem.The problem is handlers work together,the other Handler comes into play before the first Handler's time expires.When i did that with same runnable it did same thing.



    My txt file looks like:
    Ileri,5,1 /n
    Sol,10,1 /n -->10 is operation time 1 is The number of repetitions.
    Sag,5,1 /n
    Geri,10,1 /n



    My output:



    I/System.out: Ileri
    6000
    1
    I/System.out: Sol
    11000
    1
    I/System.out: Sag
    6000
    1
    I/System.out: Geri
    11000
    1
    I/System.out: A



        start_btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    timer=new Timer();

    String yon;
    String saniye1;
    String tekrar;
    int i=0,j=0,c=0,d=0;

    ArrayList<String> listS=new ArrayList<String>();

    try {
    Scanner s=new Scanner(new File("/data/data/com.example.emrecan.myapplication/files/komutlar.txt"));

    while(s.hasNextLine())
    {
    listS.add(s.nextLine());
    }

    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }

    String line2=new String[100];
    for(String str:listS)
    {
    String line=str.split(",");
    line2[c]=line[i];
    line2[c+1]=line[i+1];
    line2[c+2]=line[i+2];
    c=c+3;
    }
    //String gonderilecek=new String();
    String line3=new String[c+2];
    while(d<c)
    {
    line3[d]=line2[d];
    d=d+1;
    }
    d=0;
    tekrar=line3[2];
    int tekrar1=Integer.parseInt(tekrar);



    while(d<tekrar1)
    {

    while(j<=c-2)
    {

    yon = line3[j];
    saniye1 = line3[j + 1];
    sure1 = Long.parseLong(saniye1);
    sure1=sure1*1000;
    sure1=sure1+1000;
    System.out.println(yon);
    System.out.println(sure1);
    System.out.println(tekrar1);
    e=0;
    switch (yon)
    {
    case "Ileri":

    runnable=new Runnable() {
    @Override
    public void run() {
    sure1=sure1-1000;
    if(sure1>0)
    {
    handler.postDelayed(this,1000);
    mp.start();
    }
    else
    {
    handler.removeCallbacks(runnable);
    }
    }
    };

    handler.postDelayed(runnable,1000);
    Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();

    break;


    case "Sol":

    runnable2=new Runnable() {
    @Override
    public void run() {
    sure1=sure1-1000;
    if(sure1>0)
    {
    handler.postDelayed(this,1000);
    mp2.start();
    }
    else
    {
    handler.removeCallbacks(runnable2);
    }
    }
    };
    handler.postDelayed(runnable2,1000);
    Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
    break;


    case "Sag":


    runnable3=new Runnable() {
    @Override
    public void run() {
    sure1=sure1-1000;
    if(sure1>0)
    {
    handler.postDelayed(this,1000);
    mp3.start();
    }
    else
    {
    handler.removeCallbacks(runnable3);
    }
    }
    };
    handler.postDelayed(runnable3,1000);

    Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
    break;


    case "Geri":

    runnable4=new Runnable() {
    @Override
    public void run() {
    sure1=sure1-1000;
    if(sure1>0)
    {
    handler.postDelayed(this,1000);
    mp4.start();
    }
    else
    {
    handler.removeCallbacks(runnable4);
    }
    }
    };
    handler.postDelayed(runnable4,1000);

    Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
    break;

    default:
    Toast.makeText(getApplicationContext(), "Sıkıntı var!", Toast.LENGTH_LONG).show();
    break;

    }


    j=j+3;
    }
    j=0;
    d=d+1;
    System.out.println('A');
    }

    }
    });









    share|improve this question

























      0












      0








      0








      Hello everyone i try to get commands from txt file and I want to run these commands in order for a certain period of time.I can get the commands from the txt file correctly but when I want to do these commands using handlers for a certain period of time but there is a problem.The problem is handlers work together,the other Handler comes into play before the first Handler's time expires.When i did that with same runnable it did same thing.



      My txt file looks like:
      Ileri,5,1 /n
      Sol,10,1 /n -->10 is operation time 1 is The number of repetitions.
      Sag,5,1 /n
      Geri,10,1 /n



      My output:



      I/System.out: Ileri
      6000
      1
      I/System.out: Sol
      11000
      1
      I/System.out: Sag
      6000
      1
      I/System.out: Geri
      11000
      1
      I/System.out: A



          start_btn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      timer=new Timer();

      String yon;
      String saniye1;
      String tekrar;
      int i=0,j=0,c=0,d=0;

      ArrayList<String> listS=new ArrayList<String>();

      try {
      Scanner s=new Scanner(new File("/data/data/com.example.emrecan.myapplication/files/komutlar.txt"));

      while(s.hasNextLine())
      {
      listS.add(s.nextLine());
      }

      } catch (FileNotFoundException e) {
      e.printStackTrace();
      }

      String line2=new String[100];
      for(String str:listS)
      {
      String line=str.split(",");
      line2[c]=line[i];
      line2[c+1]=line[i+1];
      line2[c+2]=line[i+2];
      c=c+3;
      }
      //String gonderilecek=new String();
      String line3=new String[c+2];
      while(d<c)
      {
      line3[d]=line2[d];
      d=d+1;
      }
      d=0;
      tekrar=line3[2];
      int tekrar1=Integer.parseInt(tekrar);



      while(d<tekrar1)
      {

      while(j<=c-2)
      {

      yon = line3[j];
      saniye1 = line3[j + 1];
      sure1 = Long.parseLong(saniye1);
      sure1=sure1*1000;
      sure1=sure1+1000;
      System.out.println(yon);
      System.out.println(sure1);
      System.out.println(tekrar1);
      e=0;
      switch (yon)
      {
      case "Ileri":

      runnable=new Runnable() {
      @Override
      public void run() {
      sure1=sure1-1000;
      if(sure1>0)
      {
      handler.postDelayed(this,1000);
      mp.start();
      }
      else
      {
      handler.removeCallbacks(runnable);
      }
      }
      };

      handler.postDelayed(runnable,1000);
      Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();

      break;


      case "Sol":

      runnable2=new Runnable() {
      @Override
      public void run() {
      sure1=sure1-1000;
      if(sure1>0)
      {
      handler.postDelayed(this,1000);
      mp2.start();
      }
      else
      {
      handler.removeCallbacks(runnable2);
      }
      }
      };
      handler.postDelayed(runnable2,1000);
      Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
      break;


      case "Sag":


      runnable3=new Runnable() {
      @Override
      public void run() {
      sure1=sure1-1000;
      if(sure1>0)
      {
      handler.postDelayed(this,1000);
      mp3.start();
      }
      else
      {
      handler.removeCallbacks(runnable3);
      }
      }
      };
      handler.postDelayed(runnable3,1000);

      Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
      break;


      case "Geri":

      runnable4=new Runnable() {
      @Override
      public void run() {
      sure1=sure1-1000;
      if(sure1>0)
      {
      handler.postDelayed(this,1000);
      mp4.start();
      }
      else
      {
      handler.removeCallbacks(runnable4);
      }
      }
      };
      handler.postDelayed(runnable4,1000);

      Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
      break;

      default:
      Toast.makeText(getApplicationContext(), "Sıkıntı var!", Toast.LENGTH_LONG).show();
      break;

      }


      j=j+3;
      }
      j=0;
      d=d+1;
      System.out.println('A');
      }

      }
      });









      share|improve this question














      Hello everyone i try to get commands from txt file and I want to run these commands in order for a certain period of time.I can get the commands from the txt file correctly but when I want to do these commands using handlers for a certain period of time but there is a problem.The problem is handlers work together,the other Handler comes into play before the first Handler's time expires.When i did that with same runnable it did same thing.



      My txt file looks like:
      Ileri,5,1 /n
      Sol,10,1 /n -->10 is operation time 1 is The number of repetitions.
      Sag,5,1 /n
      Geri,10,1 /n



      My output:



      I/System.out: Ileri
      6000
      1
      I/System.out: Sol
      11000
      1
      I/System.out: Sag
      6000
      1
      I/System.out: Geri
      11000
      1
      I/System.out: A



          start_btn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      timer=new Timer();

      String yon;
      String saniye1;
      String tekrar;
      int i=0,j=0,c=0,d=0;

      ArrayList<String> listS=new ArrayList<String>();

      try {
      Scanner s=new Scanner(new File("/data/data/com.example.emrecan.myapplication/files/komutlar.txt"));

      while(s.hasNextLine())
      {
      listS.add(s.nextLine());
      }

      } catch (FileNotFoundException e) {
      e.printStackTrace();
      }

      String line2=new String[100];
      for(String str:listS)
      {
      String line=str.split(",");
      line2[c]=line[i];
      line2[c+1]=line[i+1];
      line2[c+2]=line[i+2];
      c=c+3;
      }
      //String gonderilecek=new String();
      String line3=new String[c+2];
      while(d<c)
      {
      line3[d]=line2[d];
      d=d+1;
      }
      d=0;
      tekrar=line3[2];
      int tekrar1=Integer.parseInt(tekrar);



      while(d<tekrar1)
      {

      while(j<=c-2)
      {

      yon = line3[j];
      saniye1 = line3[j + 1];
      sure1 = Long.parseLong(saniye1);
      sure1=sure1*1000;
      sure1=sure1+1000;
      System.out.println(yon);
      System.out.println(sure1);
      System.out.println(tekrar1);
      e=0;
      switch (yon)
      {
      case "Ileri":

      runnable=new Runnable() {
      @Override
      public void run() {
      sure1=sure1-1000;
      if(sure1>0)
      {
      handler.postDelayed(this,1000);
      mp.start();
      }
      else
      {
      handler.removeCallbacks(runnable);
      }
      }
      };

      handler.postDelayed(runnable,1000);
      Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();

      break;


      case "Sol":

      runnable2=new Runnable() {
      @Override
      public void run() {
      sure1=sure1-1000;
      if(sure1>0)
      {
      handler.postDelayed(this,1000);
      mp2.start();
      }
      else
      {
      handler.removeCallbacks(runnable2);
      }
      }
      };
      handler.postDelayed(runnable2,1000);
      Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
      break;


      case "Sag":


      runnable3=new Runnable() {
      @Override
      public void run() {
      sure1=sure1-1000;
      if(sure1>0)
      {
      handler.postDelayed(this,1000);
      mp3.start();
      }
      else
      {
      handler.removeCallbacks(runnable3);
      }
      }
      };
      handler.postDelayed(runnable3,1000);

      Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
      break;


      case "Geri":

      runnable4=new Runnable() {
      @Override
      public void run() {
      sure1=sure1-1000;
      if(sure1>0)
      {
      handler.postDelayed(this,1000);
      mp4.start();
      }
      else
      {
      handler.removeCallbacks(runnable4);
      }
      }
      };
      handler.postDelayed(runnable4,1000);

      Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
      break;

      default:
      Toast.makeText(getApplicationContext(), "Sıkıntı var!", Toast.LENGTH_LONG).show();
      break;

      }


      j=j+3;
      }
      j=0;
      d=d+1;
      System.out.println('A');
      }

      }
      });






      switch-statement media-player runnable android-handler






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 1 at 18:30









      Emre Can AYDINEmre Can AYDIN

      1




      1
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I solved my problem by using thread.sleep(); and i'm writing here because someone might need my code.



                  start_btn.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
          timer=new Timer();
          ///data/data/com.example.emrecan.myapplication/files/komutlar.txt
          String yon;
          String saniye1;
          String tekrar;
          int i=0,j=0,c=0,d=0;

          ArrayList<String> listS=new ArrayList<String>();

          try {
          Scanner s=new Scanner(new File("/data/data/com.example.emrecan.myapplication/files/komutlar.txt"));

          while(s.hasNextLine())
          {
          listS.add(s.nextLine());
          }

          } catch (FileNotFoundException e) {
          e.printStackTrace();
          }

          String line2=new String[100];
          for(String str:listS)
          {
          String line=str.split(",");
          line2[c]=line[i];
          line2[c+1]=line[i+1];
          line2[c+2]=line[i+2];
          c=c+3;
          }

          String line3=new String[c+2];
          while(d<c)
          {
          line3[d]=line2[d];
          d=d+1;
          }
          d=0;
          tekrar=line3[2];
          int tekrar1=Integer.parseInt(tekrar);

          while(d<tekrar1)
          {

          while(j<=c-2) {

          yon = line3[j];
          saniye1 = line3[j + 1];
          sure1 = Long.parseLong(saniye1);
          sure1 = sure1 * 1000;
          //sure1 = sure1 + 1000;
          System.out.println(yon);
          System.out.println(sure1);
          System.out.println(tekrar1);
          e = 0;

          while (sure1>0)
          {
          switch (yon) {
          case "Ileri":
          try {
          outputStream.write(1);
          } catch (IOException e1) {
          e1.printStackTrace();
          }
          Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
          try {
          Thread.sleep(1000); //1000 milliseconds is one second.
          } catch(InterruptedException ex) {
          Thread.currentThread().interrupt();
          }
          break;


          case "Sol":
          try {
          outputStream.write(2);
          } catch (IOException e1) {
          e1.printStackTrace();
          }

          Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
          try {
          Thread.sleep(1000); //1000 milliseconds is one second.
          } catch(InterruptedException ex) {
          Thread.currentThread().interrupt();
          }
          break;


          case "Sag":
          try {
          outputStream.write(3);
          } catch (IOException e1) {
          e1.printStackTrace();
          }
          Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
          try {
          Thread.sleep(1000); //1000 milliseconds is one second.
          } catch(InterruptedException ex) {
          Thread.currentThread().interrupt();
          }
          break;


          case "Geri":
          mp4.start();
          try {
          outputStream.write(4);
          } catch (IOException e1) {
          e1.printStackTrace();
          }

          Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
          try {
          Thread.sleep(1000); //1000 milliseconds is one second.
          } catch(InterruptedException ex) {
          Thread.currentThread().interrupt();
          }
          break;

          default:
          Toast.makeText(getApplicationContext(), "Sıkıntı var!", Toast.LENGTH_LONG).show();
          break;

          }
          sure1=sure1-1000;
          }

          j=j+3;
          }
          j=0;
          d=d+1;
          System.out.println('A');
          }
          try {
          outputStream.write(10);
          } catch (IOException e1) {
          e1.printStackTrace();
          }
          }
          });





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997915%2fmy-handlers-dont-work-smooth-how-can-i-fix-it%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I solved my problem by using thread.sleep(); and i'm writing here because someone might need my code.



                    start_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            timer=new Timer();
            ///data/data/com.example.emrecan.myapplication/files/komutlar.txt
            String yon;
            String saniye1;
            String tekrar;
            int i=0,j=0,c=0,d=0;

            ArrayList<String> listS=new ArrayList<String>();

            try {
            Scanner s=new Scanner(new File("/data/data/com.example.emrecan.myapplication/files/komutlar.txt"));

            while(s.hasNextLine())
            {
            listS.add(s.nextLine());
            }

            } catch (FileNotFoundException e) {
            e.printStackTrace();
            }

            String line2=new String[100];
            for(String str:listS)
            {
            String line=str.split(",");
            line2[c]=line[i];
            line2[c+1]=line[i+1];
            line2[c+2]=line[i+2];
            c=c+3;
            }

            String line3=new String[c+2];
            while(d<c)
            {
            line3[d]=line2[d];
            d=d+1;
            }
            d=0;
            tekrar=line3[2];
            int tekrar1=Integer.parseInt(tekrar);

            while(d<tekrar1)
            {

            while(j<=c-2) {

            yon = line3[j];
            saniye1 = line3[j + 1];
            sure1 = Long.parseLong(saniye1);
            sure1 = sure1 * 1000;
            //sure1 = sure1 + 1000;
            System.out.println(yon);
            System.out.println(sure1);
            System.out.println(tekrar1);
            e = 0;

            while (sure1>0)
            {
            switch (yon) {
            case "Ileri":
            try {
            outputStream.write(1);
            } catch (IOException e1) {
            e1.printStackTrace();
            }
            Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
            try {
            Thread.sleep(1000); //1000 milliseconds is one second.
            } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
            }
            break;


            case "Sol":
            try {
            outputStream.write(2);
            } catch (IOException e1) {
            e1.printStackTrace();
            }

            Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
            try {
            Thread.sleep(1000); //1000 milliseconds is one second.
            } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
            }
            break;


            case "Sag":
            try {
            outputStream.write(3);
            } catch (IOException e1) {
            e1.printStackTrace();
            }
            Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
            try {
            Thread.sleep(1000); //1000 milliseconds is one second.
            } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
            }
            break;


            case "Geri":
            mp4.start();
            try {
            outputStream.write(4);
            } catch (IOException e1) {
            e1.printStackTrace();
            }

            Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
            try {
            Thread.sleep(1000); //1000 milliseconds is one second.
            } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
            }
            break;

            default:
            Toast.makeText(getApplicationContext(), "Sıkıntı var!", Toast.LENGTH_LONG).show();
            break;

            }
            sure1=sure1-1000;
            }

            j=j+3;
            }
            j=0;
            d=d+1;
            System.out.println('A');
            }
            try {
            outputStream.write(10);
            } catch (IOException e1) {
            e1.printStackTrace();
            }
            }
            });





            share|improve this answer




























              0














              I solved my problem by using thread.sleep(); and i'm writing here because someone might need my code.



                      start_btn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
              timer=new Timer();
              ///data/data/com.example.emrecan.myapplication/files/komutlar.txt
              String yon;
              String saniye1;
              String tekrar;
              int i=0,j=0,c=0,d=0;

              ArrayList<String> listS=new ArrayList<String>();

              try {
              Scanner s=new Scanner(new File("/data/data/com.example.emrecan.myapplication/files/komutlar.txt"));

              while(s.hasNextLine())
              {
              listS.add(s.nextLine());
              }

              } catch (FileNotFoundException e) {
              e.printStackTrace();
              }

              String line2=new String[100];
              for(String str:listS)
              {
              String line=str.split(",");
              line2[c]=line[i];
              line2[c+1]=line[i+1];
              line2[c+2]=line[i+2];
              c=c+3;
              }

              String line3=new String[c+2];
              while(d<c)
              {
              line3[d]=line2[d];
              d=d+1;
              }
              d=0;
              tekrar=line3[2];
              int tekrar1=Integer.parseInt(tekrar);

              while(d<tekrar1)
              {

              while(j<=c-2) {

              yon = line3[j];
              saniye1 = line3[j + 1];
              sure1 = Long.parseLong(saniye1);
              sure1 = sure1 * 1000;
              //sure1 = sure1 + 1000;
              System.out.println(yon);
              System.out.println(sure1);
              System.out.println(tekrar1);
              e = 0;

              while (sure1>0)
              {
              switch (yon) {
              case "Ileri":
              try {
              outputStream.write(1);
              } catch (IOException e1) {
              e1.printStackTrace();
              }
              Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
              try {
              Thread.sleep(1000); //1000 milliseconds is one second.
              } catch(InterruptedException ex) {
              Thread.currentThread().interrupt();
              }
              break;


              case "Sol":
              try {
              outputStream.write(2);
              } catch (IOException e1) {
              e1.printStackTrace();
              }

              Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
              try {
              Thread.sleep(1000); //1000 milliseconds is one second.
              } catch(InterruptedException ex) {
              Thread.currentThread().interrupt();
              }
              break;


              case "Sag":
              try {
              outputStream.write(3);
              } catch (IOException e1) {
              e1.printStackTrace();
              }
              Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
              try {
              Thread.sleep(1000); //1000 milliseconds is one second.
              } catch(InterruptedException ex) {
              Thread.currentThread().interrupt();
              }
              break;


              case "Geri":
              mp4.start();
              try {
              outputStream.write(4);
              } catch (IOException e1) {
              e1.printStackTrace();
              }

              Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
              try {
              Thread.sleep(1000); //1000 milliseconds is one second.
              } catch(InterruptedException ex) {
              Thread.currentThread().interrupt();
              }
              break;

              default:
              Toast.makeText(getApplicationContext(), "Sıkıntı var!", Toast.LENGTH_LONG).show();
              break;

              }
              sure1=sure1-1000;
              }

              j=j+3;
              }
              j=0;
              d=d+1;
              System.out.println('A');
              }
              try {
              outputStream.write(10);
              } catch (IOException e1) {
              e1.printStackTrace();
              }
              }
              });





              share|improve this answer


























                0












                0








                0







                I solved my problem by using thread.sleep(); and i'm writing here because someone might need my code.



                        start_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                timer=new Timer();
                ///data/data/com.example.emrecan.myapplication/files/komutlar.txt
                String yon;
                String saniye1;
                String tekrar;
                int i=0,j=0,c=0,d=0;

                ArrayList<String> listS=new ArrayList<String>();

                try {
                Scanner s=new Scanner(new File("/data/data/com.example.emrecan.myapplication/files/komutlar.txt"));

                while(s.hasNextLine())
                {
                listS.add(s.nextLine());
                }

                } catch (FileNotFoundException e) {
                e.printStackTrace();
                }

                String line2=new String[100];
                for(String str:listS)
                {
                String line=str.split(",");
                line2[c]=line[i];
                line2[c+1]=line[i+1];
                line2[c+2]=line[i+2];
                c=c+3;
                }

                String line3=new String[c+2];
                while(d<c)
                {
                line3[d]=line2[d];
                d=d+1;
                }
                d=0;
                tekrar=line3[2];
                int tekrar1=Integer.parseInt(tekrar);

                while(d<tekrar1)
                {

                while(j<=c-2) {

                yon = line3[j];
                saniye1 = line3[j + 1];
                sure1 = Long.parseLong(saniye1);
                sure1 = sure1 * 1000;
                //sure1 = sure1 + 1000;
                System.out.println(yon);
                System.out.println(sure1);
                System.out.println(tekrar1);
                e = 0;

                while (sure1>0)
                {
                switch (yon) {
                case "Ileri":
                try {
                outputStream.write(1);
                } catch (IOException e1) {
                e1.printStackTrace();
                }
                Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
                try {
                Thread.sleep(1000); //1000 milliseconds is one second.
                } catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
                }
                break;


                case "Sol":
                try {
                outputStream.write(2);
                } catch (IOException e1) {
                e1.printStackTrace();
                }

                Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
                try {
                Thread.sleep(1000); //1000 milliseconds is one second.
                } catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
                }
                break;


                case "Sag":
                try {
                outputStream.write(3);
                } catch (IOException e1) {
                e1.printStackTrace();
                }
                Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
                try {
                Thread.sleep(1000); //1000 milliseconds is one second.
                } catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
                }
                break;


                case "Geri":
                mp4.start();
                try {
                outputStream.write(4);
                } catch (IOException e1) {
                e1.printStackTrace();
                }

                Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
                try {
                Thread.sleep(1000); //1000 milliseconds is one second.
                } catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
                }
                break;

                default:
                Toast.makeText(getApplicationContext(), "Sıkıntı var!", Toast.LENGTH_LONG).show();
                break;

                }
                sure1=sure1-1000;
                }

                j=j+3;
                }
                j=0;
                d=d+1;
                System.out.println('A');
                }
                try {
                outputStream.write(10);
                } catch (IOException e1) {
                e1.printStackTrace();
                }
                }
                });





                share|improve this answer













                I solved my problem by using thread.sleep(); and i'm writing here because someone might need my code.



                        start_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                timer=new Timer();
                ///data/data/com.example.emrecan.myapplication/files/komutlar.txt
                String yon;
                String saniye1;
                String tekrar;
                int i=0,j=0,c=0,d=0;

                ArrayList<String> listS=new ArrayList<String>();

                try {
                Scanner s=new Scanner(new File("/data/data/com.example.emrecan.myapplication/files/komutlar.txt"));

                while(s.hasNextLine())
                {
                listS.add(s.nextLine());
                }

                } catch (FileNotFoundException e) {
                e.printStackTrace();
                }

                String line2=new String[100];
                for(String str:listS)
                {
                String line=str.split(",");
                line2[c]=line[i];
                line2[c+1]=line[i+1];
                line2[c+2]=line[i+2];
                c=c+3;
                }

                String line3=new String[c+2];
                while(d<c)
                {
                line3[d]=line2[d];
                d=d+1;
                }
                d=0;
                tekrar=line3[2];
                int tekrar1=Integer.parseInt(tekrar);

                while(d<tekrar1)
                {

                while(j<=c-2) {

                yon = line3[j];
                saniye1 = line3[j + 1];
                sure1 = Long.parseLong(saniye1);
                sure1 = sure1 * 1000;
                //sure1 = sure1 + 1000;
                System.out.println(yon);
                System.out.println(sure1);
                System.out.println(tekrar1);
                e = 0;

                while (sure1>0)
                {
                switch (yon) {
                case "Ileri":
                try {
                outputStream.write(1);
                } catch (IOException e1) {
                e1.printStackTrace();
                }
                Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
                try {
                Thread.sleep(1000); //1000 milliseconds is one second.
                } catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
                }
                break;


                case "Sol":
                try {
                outputStream.write(2);
                } catch (IOException e1) {
                e1.printStackTrace();
                }

                Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
                try {
                Thread.sleep(1000); //1000 milliseconds is one second.
                } catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
                }
                break;


                case "Sag":
                try {
                outputStream.write(3);
                } catch (IOException e1) {
                e1.printStackTrace();
                }
                Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
                try {
                Thread.sleep(1000); //1000 milliseconds is one second.
                } catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
                }
                break;


                case "Geri":
                mp4.start();
                try {
                outputStream.write(4);
                } catch (IOException e1) {
                e1.printStackTrace();
                }

                Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
                try {
                Thread.sleep(1000); //1000 milliseconds is one second.
                } catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
                }
                break;

                default:
                Toast.makeText(getApplicationContext(), "Sıkıntı var!", Toast.LENGTH_LONG).show();
                break;

                }
                sure1=sure1-1000;
                }

                j=j+3;
                }
                j=0;
                d=d+1;
                System.out.println('A');
                }
                try {
                outputStream.write(10);
                } catch (IOException e1) {
                e1.printStackTrace();
                }
                }
                });






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 2 at 22:34









                Emre Can AYDINEmre Can AYDIN

                1




                1
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997915%2fmy-handlers-dont-work-smooth-how-can-i-fix-it%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    MongoDB - Not Authorized To Execute Command

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                    Npm cannot find a required file even through it is in the searched directory