Notification.Extra.GetString包含数据时返回空

时间:2019-06-19 02:44:55

标签: c# android xamarin xamarin.android

我的应用程序有一个NotificationListener,可以监听Spotify或GPM之类的媒体播放器通知。这是OnListenerConnected覆盖的代码:

public override void OnListenerConnected()
        {
            base.OnListenerConnected();

            Log.WriteLine(LogPriority.Info, "SmartLyrics", "OnListenerConnected (NLService): Listener connected");

            foreach (StatusBarNotification sbn in GetActiveNotifications())
            {
                if (sbn.Notification.Category == "transport")
                {
                    Log.WriteLine(LogPriority.Info, "SmartLyrics", "EXTRAS " + sbn.Notification.Extras.ToString());

                    Log.WriteLine(LogPriority.Verbose, "SmartLyrics", "Song Title - " + sbn.Notification.Extras.GetString("android.title"));
                    Log.WriteLine(LogPriority.Verbose, "SmartLyrics", "Song Artist - " + sbn.Notification.Extras.GetString("android.text"));
                }
            }
        }

问题在于,这就是我在LogCat中收到的信息:

06-18 23:36:05.405: I/SmartLyrics(25456): EXTRAS Bundle[{android.title=Kimigaite Mizuninaru, android.reduced.images=true, 
android.subText=Tadashii Itsuwarikarano Kishou, android.template=android.app.Notification$MediaStyle, 
android.showChronometer=false, android.icon=2130838406, 
android.text=ZUTOMAYO, android.progress=0, 
android.progressMax=0, android.appInfo=ApplicationInfo{34055c8 com.google.android.music} //... it goes on for a bit more}]
06-18 23:36:05.405: V/SmartLyrics(25456): Song Title - Kimigaite Mizuninaru
06-18 23:36:05.406: V/SmartLyrics(25456): Song Artist - ZUTOMAYO

06-18 23:36:05.516: I/SmartLyrics(25456): EXTRAS Bundle[{android.title=Veridis Quo, android.reduced.images=true, 
android.subText=Songs, android.template=android.app.Notification$MediaStyle, 
android.showChronometer=false, android.icon=2131231377, 
android.text=Daft Punk, android.progress=0, 
android.progressMax=0, android.appInfo=ApplicationInfo{8ca15e0 com.spotify.music} //... it goes on for a bit more}]
06-18 23:36:05.517: V/SmartLyrics(25456): Song Title - 
06-18 23:36:05.517: V/SmartLyrics(25456): Song Artist - 

(为便于阅读,对其进行了轻微编辑) 您可以看到它同时收到了Google Play音乐和Spotify的通知,但是即使Extras.ToString()显示android.titleandroid.text都有值,“ GetString”方法也不会在日志上显示任何内容。

我真的不知道怎么回事。我考虑过也许可以使用RegEx将Extras.ToString()削减到我需要的程度,但这似乎不是一个好的解决方案。这里有帮助吗?

编辑:为了澄清这一点,我已经对YouTube Vanced,GPM和Spotify进行了多次测试,只有GPM会在日志中打印信息。

0 个答案:

没有答案