gpt4 book ai didi

Android 通知重新出现

转载 作者:搜寻专家 更新时间:2023-11-01 09:45:13 25 4
gpt4 key购买 nike

我一直在尝试按照 Android Universal Music Player 示例来了解如何处理通知。

暂停音频播放会触发 stopForeground(false)我使用 false 来保留通知。

然后,如果我从最近关闭应用程序,通知就会消失,然后在重新创建 MediaBrowserCompat 浏览器时重新出现。

我的通知管理器将这一行作为它的初始值设定项。

// Cancel all notifications to handle the case where the Service was killed and
// restarted by the system.
Log.d(Constants.TAG, "Canceling all notifications");
mNotificationManager.cancelAll();

看起来该示例为这个确切的场景添加了这一行。但它似乎并没有删除“幽灵”通知。我看到调试消息“正在取消所有通知”

还有什么我应该寻找的吗?

更新:

忘记提及重新出现的通知似乎是不可交互的。播放/跳过/等。按钮什么都不做。当延迟停止处理程序运行并确定没有音频在播放时,通知最终会消失。

更新 2:

Ian 建议我监视 onStartCommand Intent 。

@Override
public int onStartCommand(Intent startIntent, int flags, int startId)
{

Log.d("start command", "On Start called");
Log.d("start command", "Start flag = " + flags);
Log.d("start command", "start Id = " + startId);

if (startIntent != null)
{
Log.d("start command", startIntent.toString());
MediaButtonReceiver.handleIntent(mSession, startIntent);
}
// Reset the delay handler to enqueue a message to stop the service if
// nothing is playing.
mDelayedStopHandler.removeCallbacksAndMessages(null);
mDelayedStopHandler.sendEmptyMessageDelayed(0, STOP_DELAY);
return START_STICKY;
}

这是我得到的日志

07-15 15:00:29.313 18901-18901/com.hackmodford.bigfinish D/start command: On Start called
07-15 15:00:29.313 18901-18901/com.hackmodford.bigfinish D/start command: Start flag = 0
07-15 15:00:29.313 18901-18901/com.hackmodford.bigfinish D/start command: start Id = 1
07-15 15:00:29.313 18901-18901/com.hackmodford.bigfinish D/start command: Intent { cmp=com.hackmodford.bigfinish/.mediaPlayerService.MediaPlayerService }
07-15 15:00:38.743 19399-19399/com.hackmodford.bigfinish D/start command: On Start called
07-15 15:00:38.743 19399-19399/com.hackmodford.bigfinish D/start command: Start flag = 0
07-15 15:00:38.743 19399-19399/com.hackmodford.bigfinish D/start command: start Id = 3

前 4 行是从开始播放开始的。然后我暂停播放并关闭最近的应用程序。通知消失,然后重新出现。然后接下来的 3 行将发布到监视器中。

更多更新:

每次在我的通知管理器中调用 .notify()、.startForeground 和 .stopForeground() 时,我都会添加日志。这是结果。

07-15 15:09:10.052 28167-28167/com.hackmodford.bigfinish D/start command: start foreground because start notification was called
07-15 15:09:10.193 28167-28167/com.hackmodford.bigfinish D/start command: On Start called
07-15 15:09:10.194 28167-28167/com.hackmodford.bigfinish D/start command: Start flag = 0
07-15 15:09:10.194 28167-28167/com.hackmodford.bigfinish D/start command: start Id = 1
07-15 15:09:10.194 28167-28167/com.hackmodford.bigfinish D/start command: Intent { cmp=com.hackmodford.bigfinish/.mediaPlayerService.MediaPlayerService }
07-15 15:09:10.210 28167-28167/com.hackmodford.bigfinish D/start command: notify because metadata changed
07-15 15:09:16.678 28167-28167/com.hackmodford.bigfinish D/start command: notify because state changed
07-15 15:09:16.764 28167-28167/com.hackmodford.bigfinish D/start command: stopForeground(false) because state is paused
07-15 15:09:16.891 28167-28167/com.hackmodford.bigfinish D/start command: notify because metadata changed
07-15 15:09:30.841 28803-28803/com.hackmodford.bigfinish D/start command: On Start called
07-15 15:09:30.841 28803-28803/com.hackmodford.bigfinish D/start command: Start flag = 0
07-15 15:09:30.841 28803-28803/com.hackmodford.bigfinish D/start command: start Id = 3

更新:我已经在我的整个项目中搜索了 startService、startForeground()、stopForeground 和 .notify() 的其他出现,但没有任何运气。日志消息描述了事件的确切顺序。

最佳答案

我可能想出了一个解决方案。

我刚刚找到了 onTaskRemove 方法。我想我可以用它来停止播放未运行的服务。

@Override
public void onTaskRemoved(Intent rootIntent)
{
if (getPlaybackState().getState() != PlaybackStateCompat.STATE_PLAYING) {
stopSelf();
}
}

如果这是个坏主意,请告诉我。

关于Android 通知重新出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38382108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com