0

我已经设置了一个接收器类接收的警报,我希望它播放铃声,振动并显示通知,然后当我单击通知时它应该将其关闭。

在显示通知的那一刻,它会振动,但铃声会永远播放,当我点击通知时,它只是再次执行整个代码,因此另一个铃声开始播放并且通知不会被关闭。

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

这是我的代码

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

2 回答 2

0

检查您的接收器是否被多次调用。还可以通过通知设置您的振动和音乐选项。你不必自己设置。请参阅此示例 http://smartandroidians.blogspot.com/2010/04/alarmmanager-and-notification-in.html

于 2013-07-12T09:07:38.613 回答
0

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

如何使用 Android 通知 API 启用振动和灯光?

于 2013-07-14T11:33:02.163 回答