我有一个更好的解决方案:
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static RemoteViews getBigContentView(Context context, Notification notification)
{
if(notification.bigContentView != null)
return notification.bigContentView;
else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
return Notification.Builder.recoverBuilder(context, notification).createBigContentView();
else
return null;
}
public static RemoteViews getContentView(Context context, Notification notification)
{
if(notification.contentView != null)
return notification.contentView;
else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
return Notification.Builder.recoverBuilder(context, notification).createContentView();
else
return null;
}
每当你提及
Notification.contentView
,只需打电话
getContentView(...)
getBigContentView(...)
Notification.bigContentView
. 您的代码将支持android nougat+,以及所有android版本。
修改后,
AddNotification
将是这样:
/**
* Add notification into recycleview
*
* @param sbn notification to add
*/
private void AddNotification(StatusBarNotification sbn)
{
Notification notification = sbn.getNotification();
if (notification == null) return;
RemoteViews remoteViews = getBigContentView(myContext(), notification);
if(remoteViews == null)
remoteViews = getContentView(myContext(), notification);
if (remoteViews != null)
{
//apply bigContentView to my recycleview list notification view
myListView.notificationview = remoteViews.apply(myContext(), myNotificationLayout);
}
//notify recycleview of a new item inserted
notifyItemInserted(0);
}