MediaPlayer类型中的方法create(Context,int)的错误不适用于参数(AlarmReceiver,int)

时间:2014-04-03 05:46:08

标签: java android

我需要解决方案:

public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "Time is up!!!!.", Toast.LENGTH_LONG).show();
    // Vibrate the mobile phone
    Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
    vibrator.vibrate(2000);
    MediaPlayer mp = MediaPlayer.create(this, R.raw.abc1);
    mp.start();
 }
}

2 个答案:

答案 0 :(得分:1)

BroadcastReceiver不是Context的子类。所以改变这个

MediaPlayer mp = MediaPlayer.create(this, R.raw.abc1);

MediaPlayer mp = MediaPlayer.create(context, R.raw.abc1);

答案 1 :(得分:1)

在'onReceive'方法中this引用AlarmReceiver,而不是活动上下文。所以改为:

MediaPlayer mp = MediaPlayer.create(context, R.raw.abc1);