1
我試圖阻止媒體播放器,並得到了我的活動類的,只要按下進程對話框的停止選項,但通過使用下面的代碼,我能夠走出我的活動類,但無法阻止媒體播放器的聲音,並且直到線程停止連續運行..如何阻止媒體播放器線程中運行
請幫我在這個問題上 感謝名單提前..
public class Test_Service extends Activity {
private MediaPlayer mp;
private Thread welcomeThread;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mp = MediaPlayer.create(Test_Service.this,R.raw.alarm1);
welcomeThread=new Thread() {
int wait = 0;
@Override
public void run() {
try {
super.run();
while (wait <30000) {
mp.start();
sleep(3000);
wait +=3000;
}
} catch (Exception e) {
System.out.println("EXc=" + e);
} finally {
}
}
};
new AlertDialog.Builder(Test_Service.this)
.setTitle("Alarm is running")
.setPositiveButton("Stop",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
mp.stop();
welcomeThread.stop();
Test_Service.this.finish();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
mp.stop();
Test_Service.this.finish();
}
})
.create().show();
welcomeThread.start();
}
}
welcomeThread.stop();不是一個好習慣,而是使用welcomeThread.interrupt(); – ingsaurabh 2011-02-17 09:16:13