我需要解决方案:
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();
}
}
答案 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);