代码不适用于新类

时间:2010-08-31 19:47:45

标签: android exception-handling

主类中的代码有效,但我需要一个其他线程,但代码无法正常工作

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 个答案:

答案 0 :(得分:1)

MediaPlayer.create需要Context作为第一个参数。 ActivityContextRunnable不是。

同样,getSystemServiceContext中定义的函数,因此您必须是Context才能像这样调用它。

您可能希望阅读AsyncTask,这是在Android后台线程中执行操作的首选方法。