在警报接收器类中使用铃声和通知

时间:2013-07-12 08:06:56

标签: android android-mediaplayer android-notifications android-alarms android-broadcast

我设置了一个由接收器类接收的闹钟,我希望它播放铃声,振动并显示通知,然后当我点击通知时它应该将其解雇。

在显示通知时,它会振动,但铃声会永久播放,当我点击通知时,它只会再次执行整个代码,因此另一个铃声开始播放,通知也不会被解除。

解决这个问题的正确方法是什么?

这是我的代码

public void onReceive(Context context, Intent intent) {

    String id = intent.getStringExtra("id");
    String type = intent.getStringExtra("type");
    String time = intent.getStringExtra("time");
    String date = intent.getStringExtra("date");

    if(type.equals("1")){
        msg = "Care UK appointment";
    }else{
        msg = "Exercise Reminder";
    }

    for(int i = 0; i < PP.parse2.size(); i++){

        if(PP.parse2.get(i).id.equals(id)){
            PP.parse2.remove(i);
            PP.saveReminder();
            break;
        }
    }

    nm = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    CharSequence from = msg;
    CharSequence message = time+" on "+date;
    PendingIntent contentIntent = PendingIntent.getBroadcast(context,
            Integer.parseInt(id), intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) context.getSystemService(Activity.ALARM_SERVICE);
    am.cancel(contentIntent);


    @SuppressWarnings("deprecation")
    Notification notif = new Notification(R.drawable.alarm_icon,
            msg, System.currentTimeMillis());
    notif.setLatestEventInfo(context, from, message, contentIntent);
    notif.flags |= Notification.FLAG_AUTO_CANCEL;
    nm.notify(1, notif);
    //nm.cancel(1);

    Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);     
    // Vibrate for 500 milliseconds
    v.vibrate(2000);
    playSound(this, getAlarmUri());

}   

private void playSound(Context context, Uri alert) {
    mMediaPlayer = new MediaPlayer();
    try {
        mMediaPlayer.setDataSource(context, alert);
        final AudioManager audioManager = (AudioManager) context
                .getSystemService(Context.AUDIO_SERVICE);
        if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
            mMediaPlayer.prepare();
            mMediaPlayer.start();

        mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {           
            @Override
            public void onCompletion(MediaPlayer mp) {
                mMediaPlayer.stop();
                mMediaPlayer.release();
        }
    }); 
        }
    } catch (IOException e) {
        System.out.println("OOPS");
    }
}

// Get an alarm sound. Try for an alarm. If none set, try notification,
// Otherwise, ringtone.
private Uri getAlarmUri() {
    Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    if (alert == null) {
        alert = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        if (alert == null) {
            alert = RingtoneManager
                    .getDefaultUri(RingtoneManager.TYPE_RINGTONE);
        }
    }
    return alert;
}

2 个答案:

答案 0 :(得分:0)

检查您的接收器是否被多次呼叫。还可以通过通知设置振动和音乐选项。你不必自己设置。参考这个样本 http://smartandroidians.blogspot.com/2010/04/alarmmanager-and-notification-in.html

答案 1 :(得分:0)

这是一个类似的问题,展示了如何在接收器中使用铃声,振动和灯光通知

How can I enable vibration and lights using the Android notifications api?