如何获取展开通知栏中的媒体播放器信息?API调用返回空列表问题
Hey there, let's figure out why you're getting an empty list from getActiveNotifications() and how to fix it to grab those media player notifications.
Why getActiveNotifications() returns an empty list
Starting from Android 11 (API level 30), regular apps can only access their own notifications using this method. If you're trying to pull notifications from other apps (like Spotify, YouTube Music, or any third-party media player), this API will return nothing—your app doesn't have the required permissions to access cross-app notifications.
Solution 1: Use NotificationListenerService (for cross-app media notifications)
This is the official, supported way to access notifications from other apps, including media players. Here's how to set it up:
Step 1: Create the listener service
Make a class that extends NotificationListenerService to listen for new notifications and filter out media ones:
class MediaNotificationListener : NotificationListenerService() { override fun onNotificationPosted(sbn: StatusBarNotification) { super.onNotificationPosted(sbn) // Check if this is a media notification val isMedia = sbn.notification.extras.getBoolean(Notification.EXTRA_IS_MEDIA_NOTIFICATION) if (isMedia) { // Extract media player details val appPackage = sbn.packageName val trackTitle = sbn.notification.extras.getString(Notification.EXTRA_TITLE) val artistName = sbn.notification.extras.getString(Notification.EXTRA_TEXT) val albumName = sbn.notification.extras.getString(Notification.EXTRA_SUB_TEXT) // Do something with this data—log it, update your UI, etc. Log.d("MediaTracker", "App: $appPackage | Track: $trackTitle | Artist: $artistName") } } override fun onNotificationRemoved(sbn: StatusBarNotification) { super.onNotificationRemoved(sbn) // Handle cases where the media notification is dismissed (e.g., player stopped) } }
Step 2: Register the service in your manifest
Add this to your AndroidManifest.xml to declare the service and request the necessary permission:
<service android:name=".MediaNotificationListener" android:label="Media Notification Tracker" android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"> <intent-filter> <action android:name="android.service.notification.NotificationListenerService" /> </intent-filter> </service>
Step 3: Prompt the user to enable access
This permission requires manual user approval in system settings. Add a helper method to open the correct settings screen:
fun openNotificationListenerSettings(context: Context) { val intent = Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS") context.startActivity(intent) }
Call this method when your app first launches or when you need to access media notifications—users have to toggle your app's permission on here.
Solution 2: For your own app's media notifications
If you only need to track media notifications from your own app, you can still use getActiveNotifications()—just filter results to your app's package:
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val activeNotifications = notificationManager.getActiveNotifications() val myMediaNotifications = activeNotifications.filter { it.packageName == packageName } if (myMediaNotifications.isNotEmpty()) { // Extract details from your app's media notification here }
Just make sure you mark your notification as a media notification when building it, using setCategory(Notification.CATEGORY_MEDIA) or Notification.EXTRA_IS_MEDIA_NOTIFICATION.
Extra Tips
- Media notifications usually have the
Notification.EXTRA_IS_MEDIA_NOTIFICATIONflag set—this is the most reliable way to identify them. - Some apps use custom notification layouts, so
EXTRA_TITLE/EXTRA_TEXTmight be empty. Parsing customRemoteViewsis tricky and app-specific, so there's no one-size-fits-all solution here. - For Android 14 (API 34+) users, you'll need to request access to specific apps' notifications if you don't want full access to all notifications.
内容的提问来源于stack exchange,提问作者anant mittal

