i'm writing a Notificator class for audio player. My notification has custom layout, so i use RemoteViews class for fill it. There are 4 ImageView (3 for buttons and 1 for cover image) and 1 TextView (for song name).
For showing my notification i use this method:
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());
}
When user clicks on Next or Previous buttons in notification my service change track and update notification using such method:
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());
}
Problem description: After several (30-40) fast (5-6 Hz) clicks on Next or Previous buttons notification freezes and stops to update.
The cause of problem is there:
remoteViews.setImageViewBitmap(R.id.album_cover, Bitmap.createScaledBitmap(cover,64,64,false));
As i understand this operation takes a lot of time. If i set size of cover in 25x25 all works pefectly. But 25x25 is too small for cover. So i have no idea how to do it properly. Help please.
UPD I've tried to use async load with Glide as described there. The result is much better, but the lags still remains.