Search code examples
androidandroid-notificationsandroid-audiomanager

Audio Manager onAudioFocusChange() not firing when notification arrives


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 !!


Solution

  • 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);