Assume I have the AOSP source code, How can I pause the APP in the foreground when pulling down the notification panel? I've googled and find an APP can listen the event onWindowFocusChange
and take some action proactively, but how can I pause ANY APP when pulling down the notification panel, without modify every APP respectively (which is impractical)?
Is there a way that I can call the onPause
function of any foreground App from the SystemUI process?
You can use below method for detecting the notification panel pull,
In your manifest file
In your activity override the
onWindowFocusChanged()
and write the below code.This uses the permission
Then in your app request for audio focus, refer below link
https://developer.android.com/guide/topics/media-apps/audio-focus#java
This will pause audio access for all other apps.
There are two parts to the solution
For detecting notification panel pull you may employ the method suggested by Akash or same code here by Lalith.
For the second part all well designed media players implement Audio Focus Change Listeners. Thus to pause all media player apps you can request temporary or full audio focus depending on whether you want to let the apps play again as soon as notification panel is collapsed.
Use following
Don't forget to implement AudioFocusChangeListener
As far as I have researched system ui process is not accessible. However, you can launch an activity with transparent theme.
In your manifest
see this and I have also personally tested creating transparent activity.
This means that the activity on top now is your transparent activity and the onPause method of the other activity will naturally be called. Once the notification panel is collapsed you can destroy this activity. And to be on safe side you can also set onTouch listener by creating an empty view that feels the screen which should destroy the activity onTouch.
Assuming that the Android device use the default MediaPlayer, according to this state diagram, you may use the native
pause
andstop
state.If you only wan't to stop sound, you may probably change the sound volume using a VolumeAutomation ?