2014-03-26 2 views
0

내 알림 소리의 지속 시간을 설정하는 데 문제가 있습니다. 여기에 지금 보이는 방법은 다음과 같습니다 당신이 볼 수 있듯이Android : 알림 소리 지속 시간

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)"는 소리를 멈추게 할뿐만 아니라 전체 알림 (조명, 진동 및 알림 서랍의 아이콘)을 중지합니다.

알림을 완전히 취소하지 않고 알림을 종료하는 방법이 있습니까?

미리 감사드립니다.

답변

1

소리 만 취소 할 방법이 없습니다. Notification 전체를 취소 한 다음 소리없이 다시 게시 할 수 있습니다. 또는 AudioManager.STREAM_NOTIFICATION 스트림 유형을 x 시간 동안 음소거 한 다음 Notification 소리가 종료 된 후에 음소거를 해제 할 수 있습니다.

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

고마워요! :) 나는 첫 번째 접근법을 좋아한다. 어떻게 진행되는지 알려 드리겠습니다. – feresr

+0

이것을 구현할 수 없습니다 : setVolumeControlStream (AudioManager.STREAM_NOTIFICATION); 나는 활동에 있지 않기 때문에 (나는 방송 수신기에있다). 두 번째 코드에 대해서는별로 도움이되지 않습니다. ( – feresr

+0

@ user1949554 [Activity.setVolumeControlStream] (http://bit.ly/1ggCVOk) – adneal

0

당신이 통지가 오면 어떻게되는지 아는가 당신은 알림 표시 줄을 터치

setVolumeControlStream(AudioManager.STREAM_NOTIFICATION); 

Notification 사운드를 끄려면 :

볼륨을 제어하려면? 알림을 취소하지 않고 소리를 중지합니다. 같은 코드를 코드에서 수행해야하는 것처럼 보입니다.