我创建了一个音乐播放器,通过服务播放音乐,并且使用startForeground()在通知中显示了诸如歌曲标题和艺术家之类的信息。这总是可以正常工作,但是最近我觉得最好倒计数播放列表中剩余的歌曲数量和时间,并在通知中显示出来。服务启动时,会启动一个线程,该线程会不断更新此信息:
private void updatePlaylistPosition() {
if (updateThread == null) {
Log.d(TAG, "Starting update thread");
updateThread = new Thread(new Runnable() {
@Override
public void run() {
try {
while (prepared) {
Log.d(TAG, "Updating playlist position");
// Set playlist position and time left.
String left = Util.formatDuration(
duration - player.getCurrentPosition());
if (songIndex < songs.size() - 1) {
left = (songs.size() - songIndex - 1) + " / " + left;
}
notificationViews.setTextViewText(R.id.tvPlaylistPosition, getString(
R.string.playlist_position, songIndex + 1, songs.size(), left));
startForeground(1, notification);
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
}
}
// Hide playlist position when playback completes.
notificationViews.setViewVisibility(R.id.tvPlaylistPosition, View.GONE);
startForeground(1, notification);
} catch (Exception ex) {
Log.e(TAG, "Error in update thread", ex);
} finally {
Log.d(TAG, "Update thread exiting");
updateThread = null;
}
}
});
updateThread.start();
} else {
Log.d(TAG, "Interrupting update thread");
updateThread.interrupt();
}
}
这是可行的,但是大约一个小时后,即使音乐仍在播放,并且该歌曲播放完毕后,下一个歌曲也会播放,因此该服务仍在运行,该通知将不再更新。
我设法在模拟器中重现了这一点,并在日志中找到了它:
07-04 10:20:37.338 2842 3149 D UPlayer : Updating playlist position
07-04 10:20:37.861 2842 3149 D UPlayer : Updating playlist position
07-04 10:20:38.391 2842 3149 D UPlayer : Updating playlist position
07-04 10:20:38.914 2842 3149 D UPlayer : Updating playlist position
07-04 10:20:39.437 2842 3149 D UPlayer : Updating playlist position
07-04 10:20:39.962 2842 3149 D UPlayer : Updating playlist position
07-04 10:20:40.488 2842 3149 D UPlayer : Updating playlist position
07-04 10:20:41.011 2842 3149 D UPlayer : Updating playlist position
07-04 10:20:41.070 1700 1712 E JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size = 124)
07-04 10:20:41.070 1700 1712 W NotificationListenerService[]: onNotificationPosted: Error receiving StatusBarNotification
07-04 10:20:41.070 1700 1712 W NotificationListenerService[]: android.os.DeadObjectException: Transaction failed on small parcel; remote process probably died
07-04 10:20:41.070 1700 1712 W NotificationListenerService[]: at android.os.BinderProxy.transactNative(Native Method)
07-04 10:20:41.070 1700 1712 W NotificationListenerService[]: at android.os.BinderProxy.transact(Binder.java:503)
07-04 10:20:41.070 1700 1712 W NotificationListenerService[]: at android.service.notification.IStatusBarNotificationHolder$Stub$Proxy.get(IStatusBarNotificationHolder.java:86)
07-04 10:20:41.070 1700 1712 W NotificationListenerService[]: at android.service.notification.NotificationListenerService$INotificationListenerWrapper.onNotificationPosted(NotificationListenerService.java:685)
07-04 10:20:41.070 1700 1712 W NotificationListenerService[]: at android.service.notification.INotificationListener$Stub.onTransact(INotificationListener.java:71)
07-04 10:20:41.070 1700 1712 W NotificationListenerService[]: at android.os.Binder.execTransact(Binder.java:453)
07-04 10:20:41.537 2842 3149 D UPlayer : Updating playlist position
07-04 10:20:41.550 2842 3149 E JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size = 1039772)
07-04 10:20:42.050 2842 3149 D UPlayer : Updating playlist position
07-04 10:20:42.063 2842 3149 E JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size = 1039988)
07-04 10:20:42.564 2842 3149 D UPlayer : Updating playlist position
07-04 10:20:42.577 2842 3149 E JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size = 1040204)
07-04 10:20:43.077 2842 3149 D UPlayer : Updating playlist position
07-04 10:20:43.090 2842 3149 E JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size = 1040420)
07-04 10:20:43.590 2842 3149 D UPlayer : Updating playlist position
07-04 10:20:43.604 2842 3149 E JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size = 1040636)
07-04 10:20:44.104 2842 3149 D UPlayer : Updating playlist position
07-04 10:20:44.120 2842 3149 E JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size = 1040852)
07-04 10:20:44.620 2842 3149 D UPlayer : Updating playlist position
07-04 10:20:44.633 2842 3149 E JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size = 1041068)
...等等...
“失败的绑定交易”和许多感叹号,听起来很严重,但我不知道这是什么意思,也找不到与Android,服务和startForeground()相关的任何信息。
可能与从线程调用startForeground()有关吗?有人吗?