2011-03-25 71 views
0

這是項目的一部分,主要關注的是當我想停止線程,然後我的進度條應該到達起點並且線程必須停止。 但是這並沒有發生在我的演示中。我無法在此使用異步任務。在這個特定的例子中,我怎樣才能停止線程? Thread.stop,notify,布爾標誌不起作用。如何停止在android中使用線程運行的進度條?

public class AudioPlayer2 extends BaseScreen implements OnClickListener { 
// private static final String TAG = "ServicesDemo"; 
    Button buttonPause, buttonStart, buttonStop,buttonForward,buttonBackward; 
//String uri; 
private final String TAG = AudioPlayer2.class.getSimpleName(); 
private String uri, albumArtUri; 
private MediaPlayer mp; 
VideoView vv; 
ImageView iv, ivAlbumArt; 
Uri albumImageUri; 
ArrayList<Track> tracks; 
static Uri sArtworkUri; 
public LinearLayout addvideo; 
private String songName, albumName; 
private TextView tvName, tvAlbum; 
private static int current_song; 
private static long album_id; 
public ProgressBar seekbar; 
public int myProgress=0; 
public MyService myservice; 
//public static double duration=0; 
String bundleDuration; 
myThread mt; 
Thread t; 
private volatile boolean flag; 
String nextDuration=null; 
long seekbarProgress=0; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.audio); 
    seekbar = (ProgressBar)findViewById(R.id.seekbar); 

    /* myservice = this.myService; 
    myservice.AP2 = AudioPlayer2.this;*/ 
    uri = this.getIntent().getExtras().getString("song_uri"); 
    buttonStart = (Button) findViewById(R.id.buttonStart); 
    buttonStop = (Button) findViewById(R.id.buttonStop); 
    buttonPause = (Button) findViewById(R.id.buttonPause); 
    tracks = dbManager.getInstance().getAllSongs(AudioPlayer2.this); 

    buttonForward = (Button) findViewById(R.id.buttonforward); 
    buttonBackward = (Button) findViewById(R.id.buttonbackward); 
    //========================================================================================= 
    sArtworkUri = Uri.parse("content://media/external/audio/albumart"); 
     Uri path = Uri.parse(uri); 
    current_song=this.getIntent().getExtras().getInt("current_song"); 
    albumArtUri = this.getIntent().getExtras().getString("album_art"); 
    bundleDuration = this.getIntent().getExtras().getString("duration"); 
    Log.d("+++++++++++++++++++Duration", bundleDuration); 
    Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart"); 
    album_id = Long.parseLong(this.getIntent().getStringExtra("album_id")); 
    int id = Integer.parseInt(this.getIntent().getStringExtra("album_id")); 

    //Toast.makeText(AudioPlayer2.this, ""+Long.parseLong(bundleDuration), Toast.LENGTH_SHORT).show(); 
     seekbar.setProgress(0); 

    albumImageUri = ContentUris.withAppendedId(sArtworkUri, album_id); 
    ivAlbumArt = (ImageView) findViewById(R.id.album_art); 

    songName = this.getIntent().getStringExtra("song_name"); 
    albumName = this.getIntent().getStringExtra("album_name"); 

    tvName = (TextView) findViewById(R.id.tv_name); 
    tvAlbum = (TextView) findViewById(R.id.tv_album); 
    if (songName != null) { 
     tvName.setText(songName); 
    } 

    if (albumName != null) { 
     tvAlbum.setText(albumName); 
    } 



    if (DataManager.getInstance().getOnPausedSong()=="y"){ 

     if(DataManager.getInstance().getPausedSongName()==songName){ 

     buttonStart.setVisibility(View.GONE); 
     buttonPause.setVisibility(View.VISIBLE); 
    } 
    } 

    stopService(new Intent(this, MyService.class)); 
    Intent intent = new Intent(this,MyService.class); 
     Bundle b = new Bundle(); 
     b.putString("song_uri",uri); 
     intent.putExtras(b); 
     startService(intent); 
     mt = new myThread(); 
     t = new Thread(mt); 
     mt.start(); 
     buttonStart.setVisibility(View.GONE); 
     buttonPause.setVisibility(View.VISIBLE); 

     //======================================================================================= 
    buttonStop.setVisibility(View.GONE); 


    buttonStart.setOnClickListener(this); 
    buttonPause.setOnClickListener(this); 
    buttonForward.setOnClickListener(this); 
    buttonBackward.setOnClickListener(this); 
    buttonStop.setOnClickListener(this); 
    buttonStart.performClick(); 
    } 
    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); 
      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); 
      } 

     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(); 
     flag=false; 
     myProgress=0; 
     bundleDuration=tracks.get(current_song).getDuration(); 
    Toast.makeText(this, tracks.get(current_song++).getDuration(), Toast.LENGTH_LONG).show(); 
     // Log.d("aaaaaaaaaaaaaaa", nextDuration); 
     //seekbar.setProgress(0); 
     myThread mt2=new myThread(); 
     mt2.start(); 
     //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.player.pause(); 

      DataManager.getInstance().setSongPause("y"); 
      DataManager.getInstance().setWantsToPlaySong(false); 
      DataManager.getInstance().setOnPausedSong("n"); 
      DataManager.getInstance().setPausedSongName(songName); 

      mt.pauseProgressBar(); 
     } 
    } 

    public void getDuration(String duration){ 
     Log.d(TAG,"BeforeToast"); 
     Log.d(TAG,"After Toast"); 
     seekbarProgress = Long.parseLong(duration); 
     seekbar.setProgress(1); 
     Log.d(TAG,"Before thread.start"); 
     Log.d(TAG,"After thread.start"); 
    } 

    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() 
       { 
         if(flag==false) 
         { 
          mt.currentThread().notify(); 
         } 
        { 
        while(!finished){ 
       while (myProgress<100){ 
       try{ 
        Log.d(TAG, "Thread Starts"); 
       // myHandle.sendMessage(myHandle.obtainMessage()); 
        myProgress++; 
        Log.d("+++++++++AA+++++++++++++",""+bundleDuration); 
        seekbar.setProgress(myProgress); 
       Thread.sleep(Long.parseLong(bundleDuration)/100); 
       Log.d(TAG,"Passed thread.sleep"); 

       } 
       catch(Throwable t){ 
        Log.e("+++++++++++++++++++++++++++", "no duration"); 
       } 
        while(paused && !finished){ 
         try{ 
          Thread.sleep(1000); //Busy wait - should really use wait/notify, but that's another lesson 
         } 
         catch(InterruptedException e){ 

         } 

       } 
        }  
       } 
        }       //=============================  
       } 
       public synchronized void pauseProgressBar(){ 
         paused = true; 
        } 

        public synchronized void unPauseProgressBar(){ 
         paused = false; 
         //call notify() here when you switch to wait/notify. 
        } 

        public synchronized void stopProgressBar(){ 
         finished = true; 
         stop(); 
         mt = null; 
         notify(); 
         // destroy(); 

        } 
       }; 

回答

0

使用的AsyncTask相反,我認爲它工作正常,如果你SendMessage函數的句柄

3

你必須使用什麼是Handler類。您可以在您停止線程的位置發送消息。 的結構是這樣的:

private class mExampleThread extends Thread{ 
    public void run(){ 
     //do cool stuff 
     mHandler.sendEmptyMessage(CONTINUE); 
     if(finish) 
      mHandler.sendEmptyMessage(STOP); 
    } 
    public void stopThread(){ 
     mHandler.removeCallbacks(mEt);} 
} 

mEt = new ExampleThread(); 

privHandlerdler mHandler = new Handler(){ 
    public void handleMessage(Message msg){ 
     switch(msg.what){ 
      case(CONTINUE) 
      mHandler.postAtTime(et); 
      break; 
      case(STOP) 
      mEt.stopThread(); 
      break; 
     } 
    } 
}; 
mEt.start(); 

,看一下這個例子:http://www.helloandroid.com/tutorials/using-threads-and-progressdialog

乾杯! David。