Domanda

Sto facendo il lettore multimediale e ho un bar di avanzamento in esso. Quando si fa clic sul pulsante di riproduzione, inizia la filettatura che imposterà il progresso della barra di avanzamento. Quando si fa clic sul pulsante Pausa, il thread si fermerà.

Ma quando premo il pulsante di arresto, voglio la barra di avanzamento per il rollback e ricominciare quando si fa clic sul gioco, quindi voglio il filo per distruggere, ma non riesco a farlo. Chiamando il metodo di sospensione () quando viene premuto il pulsante di arresto: -

 03-24 13:17:27.855: ERROR/global(26971): java.lang.UnsupportedOperationException
.

So che questo è un metodo deprecato ma ti prego come distruggere il thread quando viene premuto il pulsante di arresto.Di seguito è riportata la parte del mio codice: -


 public void onClick(View src) {
        switch (src.getId()) {
        case R.id.buttonStart:
            if(DataManager.getInstance().getSongPause()=="y"){
                myService.player.start();
                DataManager.getInstance().setSongPause("n");
                buttonStart.setVisibility(View.GONE);
                buttonPause.setVisibility(View.VISIBLE);
            //  DataManager.getInstance().setWantsToPlaySong(true);
                //DataManager.getInstance().setOnPausedSong("n");
                //DataManager.getInstance().setOnPausedSong("y");
                if(mt.paused==true){
                    mt.unPauseProgressBar();

                }
            }else{
        stopService(new Intent(this, MyService.class));
            DataManager.getInstance().setSong_uri(uri); 
            Intent intent = new Intent(this,MyService.class);
            Bundle b = new Bundle();
            b.putString("song_uri",uri );
            intent.putExtras(b);
            startService(intent);
            buttonStart.setVisibility(View.GONE);
            buttonPause.setVisibility(View.VISIBLE);
            DataManager.getInstance().setOnPausedSong("y");
            DataManager.getInstance().setPausedSongName(songName);
            //Toast.makeText(this, "My Service Started "+DataManager.getInstance().getSongDuration(), Toast.LENGTH_LONG).show();
            //new Thread(myThread).start();
            }
            //Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
            break;
        case R.id.buttonStop:
          Log.d(TAG, "onClick: stopping srvice");
          stopService(new Intent(this, MyService.class));
          buttonStart.setVisibility(View.VISIBLE);
            buttonPause.setVisibility(View.GONE);
            /*mt=null;
            seekbar.setProgress(0);*/
            mt.stopProgressBar();

            //mt.destroy();

            //mt.notify();


     break;
        case R.id.buttonforward:

             stopService(new Intent(this, MyService.class));
             current_song++;
            FetchAllMusic fetchAllMusic = new FetchAllMusic();
            fetchAllMusic.execute();
            //new Thread(myThread).start();


                break;
        case R.id.buttonbackward:
              Log.d(TAG, "onClick: stopping service");
              stopService(new Intent(this, MyService.class));
              if(current_song>0){   
              current_song--;
               }
                FetchAllMusic fetchAllMusic2 = new FetchAllMusic();
                fetchAllMusic2.execute();

              break;
        case R.id.buttonPause:
              buttonStart.setVisibility(View.VISIBLE);
                buttonPause.setVisibility(View.GONE);
            /* MyService myservice=new MyService();
             myservice.onPause();*/
                myService.player.pause();
            //  myThread.
                DataManager.getInstance().setSongPause("y");
                DataManager.getInstance().setWantsToPlaySong(false);
                DataManager.getInstance().setOnPausedSong("n");
                DataManager.getInstance().setPausedSongName(songName);

                mt.pauseProgressBar();

            /*try {
                //Thread.sleep(10000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }*/

.

e segue la mia classe di thread: -

 class myThread extends Thread implements Runnable{
            //      long duration=DataManager.getInstance().getSong_duration();
        //long value=duration/100;
          private volatile boolean paused = false;
            private volatile boolean finished = false;

                    @Override
                    public void run()

                    {

                        while(!finished){
                    while (myProgress
.

Stop Progress Bar mi dà l'errore sopra menzionato

È stato utile?

Soluzione

Non puoi usare i metodi di sospensione o distruggere i metodi per il filo stop ... Quando premuto il pulsante di arresto ..
Utilizzare questo Come fermare un thread

Controlla questo codice ... e fai come questo è il modo più sicuro per avere una bandiera in modo che il filo controlli per il suo loop principale.

class ScanningThread extends Thread {
    // Must be volatile:
    private volatile boolean stop = false;

    public void run() {
            while (!stop) {
                    System.out.println("alive");
            }
            if (stop)
                    System.out.println("Detected stop");
    }

    public synchronized void requestStop() {
            stop = true;
    }
}
public synchronized void startThread(){
    if(runner == null){
            android.util.Log.v("@@@@@@@@@@@@@@@@@@@@", "DoScan.startthread");         
            runner = new ScanningThread();
            runner.start();
    }
}
public synchronized void stopThread(){
    if(runner != null){
            android.util.Log.v("@@@@@@@@@@@@@@@@@@@@", "DoScan.stopthread");
            runner.requestStop();
            runner = null;
    }
.

}

Spero che questo aiuti ..

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top