2010-08-31 23 views
0
工作

在主类中的代码工作PERFEKT,但我需要一个其他线程但该代码将无法正常工作代码将无法在新的Class

public class Alarm implements Runnable { 


@Override 
public void run() { 

    MediaPlayer mp = new MediaPlayer().create(this, R.raw.alarm); 
    mp.start(); 
    Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 
    long milliseconds = 1000; 
    v.vibrate(milliseconds); 

} 

}

错误:

The method create(Context, int) in the type MediaPlayer is not applicable for the arguments (Alarm, int) 

The method getSystemService(String) is undefined for the type Alarm 

回答

1

MediaPlayer.create需要Context作为第一个参数。 Activity是-a ContextRunnable不是。

同样,getSystemService是在Context中定义的函数,所以您必须是Context才能像这样调用它。

您可能想了解AsyncTask,这是Android中后台线程处理事务的首选方法。

+0

为什么我需要? 是不是有一个更简单的方法? – 2010-08-31 20:48:06

+0

您是否阅读过课程概述? AsyncTask是通用的,所以你可以定义Param,Progress和Result的类型。正如它在文档中所说的,如果你不关心它们中的任何一个,你可以使用Void,Void,Void。 – 2010-08-31 21:00:59

+0

我读过它...所以我把和我可以播放声音 - >几乎完美...但我也想访问振动器,但如何? – 2010-08-31 21:43:24