簡體   English   中英

Android:通知聲音持續時間

[英]Android: Notification sound duration

嘗試設置通知警報聲音的持續時間時遇到問題。 現在是這樣的:

NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mBuilder.setAutoCancel(true);

    Uri alarmSound = Uri.parse(sharedPref.getString(
                "notifications_new_message_ringtone", ""));

    if (alarmSound != null) {
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
        try {
            mediaPlayer.setDataSource(context, alarmSound);
            mediaPlayer.prepare();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {

        }

         mBuilder.setSound(alarmSound);



        mNotificationManager.notify(notificationId, mBuilder.build());

        new Thread() {
            public void run() {

                try {
                    sleep(sharedPref.getInt("alarm_duration",2000) * DateUtils.SECOND_IN_MILLIS);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                mNotificationManager.cancel(notificationId);
            }
        }.start();
    }

如您所見,我在sharedPref變量給定的一段時間后停止了通知(用戶可以進行設置)。 關鍵是這一行:“ mNotificationManager.cancel(notificationId)”不僅會停止聲音,還會停止整個通知(燈光,振動,甚至是通知抽屜上的圖標)。

有沒有辦法只關閉通知而不完全取消通知?

提前致謝!。

沒有辦法只能取消聲音。 您可以取消整個Notification ,然后重新發布而沒有聲音。 或者,您可以將AudioManager.STREAM_NOTIFICATION流類型靜音x倍的時間,然后在Notification聲音結束后將其取消靜音。

要控制音量:

    setVolumeControlStream(AudioManager.STREAM_NOTIFICATION);

要使Notification聲音靜音:

    final AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    am.setStreamMute(AudioManager.STREAM_NOTIFICATION, true or false);

您知道收到通知並觸摸通知欄時會發生什么嗎? 它不會取消通知,只是停止其聲音。 似乎應該有一種從代碼中執行相同操作的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM