Skip to content

Commit 37b3802

Browse files
author
lizixian
committed
修复了自定义通知栏因为进度条刷新太频繁导致的 TransactionTooLargeException
1 parent 5d894fd commit 37b3802

3 files changed

Lines changed: 47 additions & 26 deletions

File tree

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ dependencies {
4545
[![](https://jitpack.io/v/EspoirX/StarrySky.svg)](https://jitpack.io/#EspoirX/StarrySky)
4646

4747
> 该版本更新了什么:<br>
48-
> 1.升级了 ExoPlayer 版本到 2.14.1<br>
49-
> 2.优化了拦截器相关逻辑和API,相关改动可看文档或者demo代码。
48+
> 修复了自定义通知栏因为进度条刷新太频繁导致的 TransactionTooLargeException(所以通知栏进度条功能要慎用哦)<br>
5049
5150
X.X.X 填的是当前的版本号。(有些人反馈说看不到版本号,版本号在 Readme 一开始就有标明,若看不到可以查看代码 gradle 文件或者加群咨询)
5251

app/src/main/res/layout/view_notify_big_play.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@
133133
</RelativeLayout>
134134
</LinearLayout>
135135

136-
<ProgressBar
136+
<!--<ProgressBar
137137
android:id="@+id/pro_notifyProgressBar"
138138
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
139139
android:layout_width="match_parent"
@@ -175,5 +175,5 @@
175175
android:layout_marginBottom="10dp"
176176
android:layout_toRightOf="@+id/line"
177177
android:text="14sp" />
178-
178+
-->
179179
</RelativeLayout>

starrysky/src/main/java/com/lzx/starrysky/notification/CustomNotification.kt

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -142,20 +142,26 @@ class CustomNotification constructor(
142142
notificationManager.cancelAll()
143143
}
144144

145-
override fun onPlaybackStateChanged(songInfo: SongInfo?, state: String,
146-
hasNextSong: Boolean, hasPreSong: Boolean) {
145+
override fun onPlaybackStateChanged(
146+
songInfo: SongInfo?, state: String,
147+
hasNextSong: Boolean, hasPreSong: Boolean
148+
) {
147149
this.hasNextSong = hasNextSong
148150
this.hasPreSong = hasPreSong
149151
playbackState = state
150152
this.songInfo = songInfo
151153
when (state) {
152154
PlaybackStage.PLAYING -> {
153-
timerTaskManager?.startToUpdateProgress()
155+
if (ID_PROGRESSBAR.getResId() != 0) {
156+
timerTaskManager?.startToUpdateProgress()
157+
}
154158
}
155159
PlaybackStage.PAUSE,
156160
PlaybackStage.ERROR,
157161
PlaybackStage.IDLE -> {
158-
timerTaskManager?.stopToUpdateProgress()
162+
if (ID_PROGRESSBAR.getResId() != 0) {
163+
timerTaskManager?.stopToUpdateProgress()
164+
}
159165
}
160166
}
161167
if (state == PlaybackStage.IDLE) {
@@ -319,14 +325,20 @@ class CustomNotification constructor(
319325
bigRemoteView?.setImageViewResource(ID_IMG_NOTIFY_PLAY_OR_PAUSE.getResId(), name.getResDrawable())
320326
}
321327
//设置喜欢或收藏按钮
322-
bigRemoteView?.setImageViewResource(ID_IMG_NOTIFY_FAVORITE.getResId(),
323-
isDark.getResDrawableByDark(DRAWABLE_NOTIFY_BTN_DARK_FAVORITE, DRAWABLE_NOTIFY_BTN_LIGHT_FAVORITE))
328+
bigRemoteView?.setImageViewResource(
329+
ID_IMG_NOTIFY_FAVORITE.getResId(),
330+
isDark.getResDrawableByDark(DRAWABLE_NOTIFY_BTN_DARK_FAVORITE, DRAWABLE_NOTIFY_BTN_LIGHT_FAVORITE)
331+
)
324332
//设置歌词按钮
325-
bigRemoteView?.setImageViewResource(ID_IMG_NOTIFY_LYRICS.getResId(),
326-
isDark.getResDrawableByDark(DRAWABLE_NOTIFY_BTN_DARK_LYRICS, DRAWABLE_NOTIFY_BTN_LIGHT_LYRICS))
333+
bigRemoteView?.setImageViewResource(
334+
ID_IMG_NOTIFY_LYRICS.getResId(),
335+
isDark.getResDrawableByDark(DRAWABLE_NOTIFY_BTN_DARK_LYRICS, DRAWABLE_NOTIFY_BTN_LIGHT_LYRICS)
336+
)
327337
//设置下载按钮
328-
bigRemoteView?.setImageViewResource(ID_IMG_NOTIFY_DOWNLOAD.getResId(),
329-
isDark.getResDrawableByDark(DRAWABLE_NOTIFY_BTN_DARK_DOWNLOAD, DRAWABLE_NOTIFY_BTN_LIGHT_DOWNLOAD))
338+
bigRemoteView?.setImageViewResource(
339+
ID_IMG_NOTIFY_DOWNLOAD.getResId(),
340+
isDark.getResDrawableByDark(DRAWABLE_NOTIFY_BTN_DARK_DOWNLOAD, DRAWABLE_NOTIFY_BTN_LIGHT_DOWNLOAD)
341+
)
330342
//上一首下一首按钮
331343
disableNextBtn(hasNextSong, isDark)
332344
disablePreviousBtn(hasPreSong, isDark)
@@ -418,7 +430,7 @@ class CustomNotification constructor(
418430
mStarted = true
419431
}
420432
}
421-
if (timerTaskManager == null) {
433+
if (timerTaskManager == null && ID_PROGRESSBAR.getResId() != 0) {
422434
timerTaskManager = TimerTaskManager()
423435
timerTaskManager?.setUpdateProgressTask {
424436
val player = (context as MusicService).binder?.player
@@ -434,7 +446,7 @@ class CustomNotification constructor(
434446
}
435447
}
436448
val player = (context as MusicService).binder?.player
437-
if (player?.isPlaying() == true && timerTaskManager?.isRunning() == false) {
449+
if (player?.isPlaying() == true && timerTaskManager?.isRunning() == false && ID_PROGRESSBAR.getResId() != 0) {
438450
timerTaskManager?.startToUpdateProgress()
439451
}
440452
}
@@ -450,8 +462,10 @@ class CustomNotification constructor(
450462
}
451463
(context as MusicService).stopForeground(true)
452464
}
453-
timerTaskManager?.removeUpdateProgressTask()
454-
timerTaskManager = null
465+
if (ID_PROGRESSBAR.getResId() != 0) {
466+
timerTaskManager?.removeUpdateProgressTask()
467+
timerTaskManager = null
468+
}
455469
}
456470

457471
override fun onCommand(command: String?, extras: Bundle?) {
@@ -477,12 +491,16 @@ class CustomNotification constructor(
477491
val isDark = colorUtils.isDarkNotificationBar(context, mNotification!!)
478492
//喜欢或收藏按钮选中时样式
479493
if (isFavorite) {
480-
bigRemoteView?.setImageViewResource(ID_IMG_NOTIFY_FAVORITE.getResId(),
481-
DRAWABLE_NOTIFY_BTN_FAVORITE.getResDrawable())
494+
bigRemoteView?.setImageViewResource(
495+
ID_IMG_NOTIFY_FAVORITE.getResId(),
496+
DRAWABLE_NOTIFY_BTN_FAVORITE.getResDrawable()
497+
)
482498
} else {
483499
//喜欢或收藏按钮没选中时样式
484-
bigRemoteView?.setImageViewResource(ID_IMG_NOTIFY_FAVORITE.getResId(),
485-
isDark.getResDrawableByDark(DRAWABLE_NOTIFY_BTN_DARK_FAVORITE, DRAWABLE_NOTIFY_BTN_LIGHT_FAVORITE))
500+
bigRemoteView?.setImageViewResource(
501+
ID_IMG_NOTIFY_FAVORITE.getResId(),
502+
isDark.getResDrawableByDark(DRAWABLE_NOTIFY_BTN_DARK_FAVORITE, DRAWABLE_NOTIFY_BTN_LIGHT_FAVORITE)
503+
)
486504
}
487505
if (mNotification != null) {
488506
notificationManager?.notify(NOTIFICATION_ID, mNotification)
@@ -499,12 +517,16 @@ class CustomNotification constructor(
499517
val isDark = colorUtils.isDarkNotificationBar(context, mNotification!!)
500518
//歌词按钮选中时样式
501519
if (isChecked) {
502-
bigRemoteView?.setImageViewResource(ID_IMG_NOTIFY_LYRICS.getResId(),
503-
DRAWABLE_NOTIFY_BTN_LYRICS.getResDrawable())
520+
bigRemoteView?.setImageViewResource(
521+
ID_IMG_NOTIFY_LYRICS.getResId(),
522+
DRAWABLE_NOTIFY_BTN_LYRICS.getResDrawable()
523+
)
504524
} else {
505525
//歌词按钮没选中时样式
506-
bigRemoteView?.setImageViewResource(ID_IMG_NOTIFY_LYRICS.getResId(),
507-
isDark.getResDrawableByDark(DRAWABLE_NOTIFY_BTN_DARK_LYRICS, DRAWABLE_NOTIFY_BTN_LIGHT_LYRICS))
526+
bigRemoteView?.setImageViewResource(
527+
ID_IMG_NOTIFY_LYRICS.getResId(),
528+
isDark.getResDrawableByDark(DRAWABLE_NOTIFY_BTN_DARK_LYRICS, DRAWABLE_NOTIFY_BTN_LIGHT_LYRICS)
529+
)
508530
}
509531
if (mNotification != null) {
510532
notificationManager?.notify(NOTIFICATION_ID, mNotification)

0 commit comments

Comments
 (0)