views:

30

answers:

1

Hello!! 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);

}

}

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
+1  A: 

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.

Mayra
why do i need <URL, Integer, Long>?isn't there a easier way?
Hans Wurst
Did you read the class overview? AsyncTask is generic, so you can define the types for Param, Progress and Result. As it says in the document, you can use Void, Void, Void if you don't care about any of them.
Mayra
i read it ... so i took <MediaPlayer, Vibrator, String> and i can play the sound --> nearly perfekt... but i also want to access the Vibrator but how?
Hans Wurst
Access it where? By passing in class type Vibrator as the second generic parameter, you are saying that progress of your async tasked will be announced via that class (publicProgress function expects a parameter of type Vibrator). I don't think that is what you want, but I'm not sure what it is you want to accomplish.
Mayra