2011-02-01 40 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霍普的意见。