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