我正在为音频播放器编写Notificator类。我的通知有自定义布局,所以我使用RemoteViews类来填充它。有4个ImageView(3个用于按钮,1个用于封面图像)和1个TextView(用于歌曲名称)。
为了显示我的通知我使用这种方法:
public void notify(final Context context) {
mContext = context;
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
remoteViews = new RemoteViews(context.getPackageName(),
R.layout.notification);
remoteViews.setImageViewResource(R.id.play,R.drawable.btn_pause_notification);
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Intent playPreviousIntent = new Intent(MusicService.ACTION_PREVIOUS);
PendingIntent pendingPlayPreviousIntent = PendingIntent.getBroadcast(context, 0, playPreviousIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.previous,pendingPlayPreviousIntent);
Intent playPauseIntent = new Intent(MusicService.ACTION_PLAYPAUSE);
PendingIntent pendingPlayPauseIntent = PendingIntent.getBroadcast(context, 0, playPauseIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.play,pendingPlayPauseIntent);
Intent playNextIntent = new Intent(MusicService.ACTION_NEXT);
PendingIntent pendingPlayNextIntent = PendingIntent.getBroadcast(context, 0, playNextIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.next,pendingPlayNextIntent);
Intent cancelIntent = new Intent(MusicService.CANCEL_NOTIFICATION);
PendingIntent pendingCancelIntent = PendingIntent.getBroadcast(context, 0, cancelIntent, 0);
builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_play_arrow_black)
.setAutoCancel(true)
.setOngoing(true)
.setOnlyAlertOnce(true)
.setContentIntent(pIntent);
builder.setContent(remoteViews);
builder.setDeleteIntent(pendingCancelIntent);
mNotificationManager.notify(NOTIFICATION_ID,builder.build());
}
当用户点击通知中的“下一个”或“上一个”按钮时,我的服务会使用以下方法更改跟踪和更新通知:
public void updateInfo(Context context, Bitmap cover, String title, String artist) {
remoteViews.setImageViewBitmap(R.id.album_cover, Bitmap.createScaledBitmap(cover,64,64,false));
String text;
if(title == "")
text = context.getString(R.string.unknown_song);
else
text = title;
text += " - ";
if(artist == "")
text += context.getString(R.string.unknown_artist);
else
text += artist;
remoteViews.setTextViewText(R.id.song,text);
mNotificationManager.notify(NOTIFICATION_ID,builder.build());
}
问题描述: 在几次(30-40)快速(5-6 Hz)点击“下一个”或“上一个”按钮后,通知会冻结并停止更新。
问题的原因在于:
remoteViews.setImageViewBitmap(R.id.album_cover, Bitmap.createScaledBitmap(cover,64,64,false));
据我所知,此操作需要花费很多时间。如果我在25x25中设置封面尺寸,那么所有作品都是完美的。但25x25对于封面来说太小了。所以我不知道如何正确地做到这一点。请帮助。
UPD 我尝试使用Glide的异步加载,如there所述。结果好多了,但滞后仍然存在。