Search code examples
switch-statementmedia-playerrunnableandroid-handler

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


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');
                }

        }
    });

Solution

  • 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();
                }
            }
        });