Search code examples
androidandroid-mediaplayerandroid-notificationsandroid-alarmsandroid-broadcast

Using ringtone and notification in an alarm receiver class


I have set up an alarm which is received by a receiver class, I want it to play a ringtone, vibrate and display a notification then when I click the notification it should dismiss it.

At the moment the the notification is displayed, it vibrates but the ringtone plays forever and when I click on the notification it just executes the entire code again so another ringtone starts to play and the notification isn't dismissed.

What is the correct way to go about this?

Here is my code

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;
}

Solution

  • Here is a similar issue that shows how to use ringtone, vibration and lights through, notifications in a receiver

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