开发者

Code won't work in new Class

开发者 https://www.devze.com 2023-01-13 18:00 出处:网络
the code in the main class works perfekt but i need a other thread but there the code won\'t work public class Alarm implements Runnable {

the code in the main class works perfekt but i need a other thread but there the code won't work

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)开发者_如何学Go;

}

}

Errors:

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

and

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


MediaPlayer.create requires a Context as the first parameter. An Activity is-a Context, a Runnable is not.

Likewise, getSystemService is a function defined in Context, so you must be a Context to call it like that.

You might want to read up on AsyncTask, the preferred method for doing things in a background thread in Android.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号