2011-02-01 39 views
0

我得到了一些答案,說幹就幹,並嘗試了以下內容:仍然有問題結束線程

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.

誰能幫助我在這裏。我覺得這是結束線程一個不錯的選擇,但同樣,我得到一些不可逾越的錯誤...

+0

你確定它的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

+0

出於某種原因,喜歡把那些小寫。它是onPause(),但格式改變了。 – user571866 2011-02-01 23:24:54

回答

1

當您使用匿名類,唯一的方法,你可以從類本身是在那些對外宣稱訪問它的超類型。在你的情況下,類之外的任何代碼只能調用run()方法,因爲這是爲Runnable定義的。解決方案是聲明一個顯式類,這是你試圖用MyRunnable做的事情。

錯誤消息,不過,建議你沒有得到它完全正確。特別是,「類型不匹配」錯誤說MyRunnable沒有實現Runnable。該消息必須來自與您發佈的代碼不同的代碼。此外,顯然無論您嘗試使用哪種對象調用stopThread,編譯器在那一刻都只知道它是Runnable而不是MyRunnable。

在一個單獨的說明,一旦你過去的編譯器錯誤,你不會獲得幸福是如何運行的。您將發送空的FIRE消息與CPU可以產生它們一樣快。您需要稍微減慢一下,或許通過適當的延遲調用Thread.sleep()。

+0

我聲明瞭r,我使用runnable作爲:private MyRunnable r;所以它應該註冊爲myRunnable,但不是。我不知道爲什麼。至於添加Thread.sleep,是像Thread.sleep(incriment)或:thread.sleep(2000)那樣讓它減慢速度。我用這個在屏幕上替換一種顏色,我想快速閃爍,但不是瘋狂的。我很感激你的幫助,以及你可能擁有的更多洞察力。 – user571866 2011-02-01 23:19:33

0

以下爲我編譯罰款:

private MyRunnable r; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    r = new MyRunnable();// this needs to create a new MyRunnable 
} 
private class MyRunnable implements Runnable { 

    private boolean doRun = true; 
    @Override 
    public void run() { 
     // do stuff 
    } 
    public void stopThread(){ 
     doRun = false; 
    } 
} 
@Override 
protected void onPause(){ 
    r.stopThread(); 
} 
@Override 
protected void onDestroy() { 
    r.stopThread(); 
} 

你應該記住對此解決方案的性能承擔@Ted霍普的意見。