我得到了一些答案,說幹就幹,並嘗試了以下內容:仍然有問題結束線程
01 import android.app.Activity;
02 import android.media.MediaPlayer;
03 import android.os.Bundle;
04 import android.os.Handler;
05 import android.os.Message;
06 import android.media.MediaPlayer;
07 import android.media.AudioManager;
08 import android.content.Context;
09 import java.lang.Runnable;
10
11 public class CanvasDrawingActivity extends Activity {
12
13 private static final int FIRE = 0;
14 private int initVolume = 0;
15 private Handler handler;
16 private MyCanvas v;
17 private MediaPlayer mp;
18 private AudioManager am;
19 private MyRunnable r;// this is our custom runnable!
20
21 @Override
22 public void onCreate(Bundle savedInstanceState) {
23 super.onCreate(savedInstanceState);
24
25 am = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
26
27 // this method gets the current volume setting for music
28 initVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
29
30 // this method sets the volume for music | the 100 is the volume. you can put there either initVolume or whatever value you want
31 am.setStreamVolume(AudioManager.STREAM_MUSIC,100,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
32
33 mp = MediaPlayer.create(this, R.raw.siren_1);
34
35 makeHandler();
36 v =new MyCanvas(this);
37 setContentView(v);
38 r = new MyRunnable();// this needs to create a new MyRunnable
39 new Thread(r).start();
40 mp.setLooping(true);
41 mp.start();
42 }
43 private void makeHandler()
44 {
45 handler = new Handler(){
46
47 @Override
48 public void handleMessage(Message msg) {
49 switch(msg.what)
50 {
51 case FIRE:
52 {
53 v.invalidate();
54 break;
55 }
56 }
57 }
58
59 };
60
61 }
62 private class MyRunnable extends Runnable {// you had this in the wrong spot...
63 private boolean doRun = true;
64
65 @Override
66 public void run(){
67 while(doRun)
68 handler.sendEmptyMessage(FIRE);
69 }
70 public void stopThread(){
71 doRun = false;
72 }
73 }
74 protected void onpause() {
75 super.onpause();
76 mp.stop();
77 r.stopThread();
78 finish();
79 }
80 protected void onfinish() {
81 mp.stop();
82 r.stopThread();
83 finish();
84 }
85
86 }
我得到一個錯誤,可運行不可能是超類。具體來說,runnable類型不能是MyRunnable的超類;超類必須是一個類。
然後在的onPause和onfinish它給出了錯誤:TE法stopThread是未定義的類型運行的。即使擴展改爲實現時也會發生這種情況。
我也試過:
01 Runnable MyRunnable = new Runnable(){
02
03
04 private boolean doRun = true;
05
06 @Override
07 public void run(){
08 while(doRun)
09 handler.sendEmptyMessage(FIRE);
10 }
11 public void stopThread(){
12 doRun = false;
13 }
14 }
我也試過:
private class MyRunnable implements Runnable \\etc
掃清了可運行的問題,但會導致我的
r = new MyRunnable();
和
r.stopThread();
是錯誤的還是它說: 的stopThread is undefined for the type runnable, and Type mismatch cannot convert from CanvasDrawingActivity.MyRunnable to Runnable.
誰能幫助我在這裏。我覺得這是結束線程一個不錯的選擇,但同樣,我得到一些不可逾越的錯誤...
你確定它的onPause或[的onPause()](http://developer.android.com/reference/android/app/Activity.html#onPause())也可以參考[這裏](HTTP:// stackoverflow.com/questions/1204012/why-isnt-the-thread-stopping) – Reno 2011-02-01 05:29:38
出於某種原因,喜歡把那些小寫。它是onPause(),但格式改變了。 – user571866 2011-02-01 23:24:54