0

I want to perform some action before a notification sound occurs. After some googling I found out that OnAudioFocusChangeListener is the way to go. I made my service implement AudioManager.OnAudioFocusChangeListener but the method onAudioFocusChange() is never called.

public void onAudioFocusChange(int focusChange) {
        Log.d(LOG_TAG, "Called!");
        if(focusChange == AudioManager.AUDIOFOCUS_GAIN){
              // Do STuff
        }
}

Can someone please help me, any advice is appreciated !!

varunkr
  • 5,364
  • 11
  • 50
  • 99

1 Answers1

1

AudioManager.OnAudioFocusChangeListener must be registered to your AudioManager in order to be called properly.

//get the audiomanager
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);


    //create the listener
      audioFocusListener=new AudioManager.OnAudioFocusChangeListener() {
                @Override
                public void onAudioFocusChange(int focusChange) {
    
                    // do your job here
                    if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
    
                    } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
    
                    } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
                       
    
                    }
                }
            };
    
    //register the listener
    audioManager.requestAudioFocus(audioFocusListener,
                            AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    
    //later unregister it when you do not need it anymore
    AppContext.audioManager.abandonAudioFocus(audioFocusListener);

You can try this out with e.g. the Youtube app, that will trigger this listener.

Edit

NotificationListenerService

AndroidManifest.xml

<service
    android:name=".NotificationListenerService"
    android:label="@string/app_name"
    android:enabled="true"
    android:exported="true"
    android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
    <intent-filter>
        <action android:name="android.service.notification.NotificationListenerService" />
    </intent-filter>
</service>

NotificationListenerService.java:

public class NotificationListenerService extends android.service.notification.NotificationListenerService {
    private static final String TAG = "NotificationListenerService";
    public NotificationListenerService(){}
    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
       // do your job
    } 



}

To enable the listener you can go to:

Settings >  Sound & Notification > Notification Access > App > Enable

or call this from your app:

  Intent i=new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
                    startActivity(i);
Community
  • 1
  • 1
csenga
  • 3,819
  • 1
  • 21
  • 31
  • Thnx for the reply, but I have already registered it in the onCreate function of my service as AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE); – varunkr Dec 11 '15 at 14:28
  • and did you call requestAudioFocus with the proper STREAM ? e.g. STREAM_NOTIFICATION – csenga Dec 11 '15 at 14:30
  • i did this in onCreate AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE); audioManager.requestAudioFocus(this,AudioManager.STREAM_NOTIFICATION,AudioManager.AUDIOFOCUS_GAIN); – varunkr Dec 11 '15 at 14:32
  • Are other streams working as they should? e.g. AudioManager.STREAM_MUSIC with the Youtube app – csenga Dec 11 '15 at 14:37
  • To my surprise, yes it did, just tried with youtube !! – varunkr Dec 11 '15 at 14:39
  • 1
    http://developer.android.com/intl/es/reference/android/app/Notification.Builder.html#setSound(android.net.Uri, int) here it says notification can play it's sound on different streams, so in theory AudioManager.STREAM_NOTIFICATION is not sufficient for all case. What is more, i'm wondering abouth whether the notifications handle the audio focus properly. – csenga Dec 11 '15 at 14:51
  • yeah that was what I was thinking too, do you have any idea how to handle the notification case? – varunkr Dec 11 '15 at 14:53
  • You can use NotificationListenerService. – csenga Dec 11 '15 at 14:54
  • I post a snippet here with NotificationListenerService. – csenga Dec 11 '15 at 14:56
  • 1
    Thanks for the help dude, but onNotificationPosted will not work for me, because I do not want the sound to be played, I want to do some action before the sound is played. I however used STREAM_RING and it did work, Thanks, I suggest you to add that comment to the answer too !! – varunkr Dec 11 '15 at 15:05
  • Though the audio did get focus and hence I accepted your answer but my problem is still there, the sound plays as soon as the focus is gained, do you have any idea how can I perform some action before the sound plays? – varunkr Dec 11 '15 at 15:09
  • what kind of action do you want to perform? – csenga Dec 11 '15 at 15:11
  • I want to check some info about the notification and depending on the result I will or will not play the sound, I want to make that check before the sound plays – varunkr Dec 11 '15 at 15:12
  • 1
    As i thought this can't be done, you can not access the notification before it has been "notified". http://stackoverflow.com/a/29974210/3862022 – csenga Dec 11 '15 at 15:23