不能破坏线程

发布于 2024-10-26 12:47:30 字数 4259 浏览 0 评论 0原文

我正在制作媒体播放器,里面有进度条。 单击播放按钮时,线程启动,这将设置进度条的进度。 当单击暂停按钮时,线程将停止。

但是当我按下停止按钮时,我希望进度条回滚并在单击播放时重新开始,因此我希望线程被销毁,但我无法做到这一点。 按下停止按钮时调用 suspend() 方法会给出:-

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

我知道这是一个已弃用的方法,但请指导我如何在按下停止按钮时销毁线程。以下是我的代码的一部分:-


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

以下是我的线程类:-

 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

停止进度栏给了我上述错误

I am making media player and i have progress bar in it.
When play button is clicked then the thread starts which will set progress of progress bar.
when pause button is clicked the the thread will stop.

but when i press stop button i want the progress bar to rollback and start again when play is clicked hence i want the thread to destroy but i am unable to do it .
Calling suspend() method when stop button is pressed gives :-

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

i know this is a deprecated method but please guid me how to destroy the thread when stop button is pressed. Following is the part of my code :-


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

and Following is my thread class :-

 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 gives me the above mentioned error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

轮廓§ 2024-11-02 12:47:30

你不能使用挂起或销毁方法来停止线程......
当停止按钮按下时..
使用此 如何停止线程

检查此代码...并这样做是拥有标志的最安全方法,以便线程在其主循环内检查它。

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

希望

这有帮助..

U cant use either suspend or destroy methods for thread stop...
when stop button pressed..
use this How to stop a thread

Check this code...and do like this is the safest way to have a flag so the thread checks for it inside its main loop.

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

}

Hope this helps..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文