这是项目的一部分,主要关注的是当我想要停止线程然后我的进度条应该到达起点并且线程必须停止。 但是我的演示中没有发生这种情况。我不能在这里使用Async Task。如何在这个特定的例子中停止线程? Thread.stop,notify,boolean flags不起作用。
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 :(得分: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
干杯! 大卫。
答案 1 :(得分:0)
使用ASyncTask代替,我认为如果你将sendMessage发送到句柄
它会正常工作