Couldn't expand RemoteViews for: StatusBarNotification
支持布局:FrameLayout, LinearLayout, RelativeLayout
支持组件:AnalogClock, Button, Chronometer, ImageButton, ImageView, ProgressBar, TextView, ViewFlipper, ListView, GridView, StackView, AdapterViewFlipper
AppCompatImageView继承ImageView但不支持(填坑)
网络图片可使用 Glide NotificationTarget
更新
this.manager = NotificationManagerCompat.from(activity);
private Notification buildNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(activity, "");
Intent intent = new Intent(activity, MainActivity.class);
intent.putExtra(Extras.EXTRA_NOTIFICATION, true);
intent.setAction(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent startMainActivity = PendingIntent.getActivity(activity, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews large = createContentBigView();
RemoteViews small = createContentView();
builder.setContentIntent(startMainActivity)
.setOngoing(true)
.setShowWhen(false)
.setSmallIcon(R.drawable.ic_music_logo)
.setCustomBigContentView(large)
.setCustomContentView(small)
.setVisibility(Notification.VISIBILITY_PUBLIC);
Notification notification = builder.build();
largeTarget = createNotifyTarget(large, notification);
smallTarget = createNotifyTarget(small, notification);
return notification;
}
private NotificationTarget createNotifyTarget(RemoteViews remoteViews, Notification notification) {
return new NotificationTarget(activity, R.id.iv_cover, remoteViews, notification, PLAY_NOTIFY_ID);
}
private void updateNotify(String url, NotificationTarget target) {
Glide.with(activity.getApplicationContext())
.asBitmap()
.load(url)
.apply(bitmapTransform(
new RoundedCornersTransformation(15, 0, RoundedCornersTransformation.CornerType.LEFT)))
.apply(new RequestOptions().placeholder(R.drawable.ic_default_music)
.error(R.drawable.ic_default_music))
.into(target);
}
/**
* 图片,歌名,艺术家,播放按钮,下一曲按钮,关闭按钮
*
* @param view v
*/
private void setCommonView(final RemoteViews view) {
final String name = currentSong.getTitle();
final String arts = currentSong.getArtist();
view.setTextViewText(R.id.title1, name);
view.setTextViewText(R.id.sub_title, arts + " - " + name);
if (TextUtils.isEmpty(currentSong.getAlbum_path())) {
view.setImageViewResource(R.id.iv_cover, R.drawable.ic_default_music);
}
view.setImageViewResource(R.id.image_play_pause, play ? R.drawable.ic_pause : R.drawable.ic_play);
view.setImageViewResource(R.id.image_prev, R.drawable.ic_previous);
view.setImageViewResource(R.id.image_next, R.drawable.ic_next);
}