概述
最近在写一个音乐类APP时需要从Notification发送广播给Service执行操作,但是在BroadcaseReceiver中出现onReceive()被多次调用的问题。网上搜索了很久都没找到解决方案,但也在搜索过程中收到了一些启发,于是决定把广播接收器变成单例模式,没想到效果一级棒。
/**
* 广播接收器部分,单例模式
*/
class NotificationReceiver private constructor() :
BroadcastReceiver() {
companion object {
const val PREVIOUS = "previous"
const val PAUSE = "pause"
const val PLAY = "play"
const val NEXT = "next"
val default = NotificationReceiver()
}
private var weakReference: WeakReference<MediaPlayerService>? = null
override fun onReceive(context: Context?, intent: Intent?) {
Log.d(TAG, "收到广播了,intentAction:${intent?.action}")
when (intent?.action) {
PREVIOUS -> {
weakReference?.get()?.previous()
}
PLAY -> {
weakReference?.get()?.play(false)
}
NEXT -> {
weakReference?.get()?.next()
}
PAUSE -> {
weakReference?.get()?.pause()
}
}
}
fun setWeakReference(weakReference: WeakReference<MediaPlayerService>) {
this.weakReference = weakReference
}
}
下面的MyMediaSession类用于演示注册广播
private class MyMediaSession(private val mContext: Context) {
......
private var receiver : NotificationReceiver? = null
....
// 在实例化时就把广播注册了
init {
// 注册广播
if (receiver == null) {
receiver = NotificationReceiver.default
val filter = IntentFilter()
filter.addAction(NotificationReceiver.PAUSE)
filter.addAction(NotificationReceiver.PLAY)
filter.addAction(NotificationReceiver.PREVIOUS)
filter.addAction(NotificationReceiver.NEXT)
mContext.registerReceiver(receiver, filter)
}
}
....
private fun unregister() {
mContext.unregisterReceiver(receiver)
}
....
}
这样就只能获取到一个广播接收器实例,加上receiver == null 的判断确保只注册一次
最后
以上就是失眠吐司为你收集整理的【安卓开发】单例模式解决Broadcast广播重复接收的全部内容,希望文章能够帮你解决【安卓开发】单例模式解决Broadcast广播重复接收所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复