我试图显示类似这样的紧凑通知(展开和折叠的通知):
mIcon = R.drawable.ic_launcher;
Notification noti = new Notification.Builder(mContext)
.setContentTitle(mContentTitle)
.setContentText(mContentText)
.setSmallIcon(mIcon)
.setLargeIcon(mIcon2)
.setStyle(new Notification.BigTextStyle()
.bigText(mContentText))
.build();我收到以下错误:
Call requires API level 16 (current min is 11): android.app.Notification.Builder#setStyle
The method setLargeIcon(Bitmap) in the type Notification.Builder is not applicable for the arguments (int)首先,如何执行检查当前设备上是否可用setStyle的条件,如果没有显示正常通知?
第二,如何初始化mIcon2,使其具有与mIcon相同的图标,因为它不需要int?
第三,构建后如何实际触发通知出现?它和老的一样吗?
// show the notification
mNotificationManager.notify(1, noti);第四,bigText的最大字符数是多少?
发布于 2014-03-19 21:49:32
因此,这确实取决于您想要使用的具体细节,但是这里是我工作的应用程序中的一个示例。
请注意,我只尝试加载图像,如果它将被使用,否则,我浪费内存和下载时间。
该示例显示了BigPicture样式,但您也可以从文档中获得收件箱或BigText样式。
// here is the base of a notification
NotificationCompat.Builder b = new NotificationCompat.Builder(context);
b.setSmallIcon(R.drawable.actionbar_icon);
b.setContentTitle(title);
b.setContentText(Html.fromHtml(msg));
b.setTicker(title);
b.setWhen(System.currentTimeMillis());
// notification shade open icon
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && url != null)
try {
b.setLargeIcon(Picasso.with(context).load(url)
.resizeDimen(android.R.dimen.notification_large_icon_width,
android.R.dimen.notification_large_icon_width).centerCrop().get());
} catch (IOException e) {
Log.e(this, "Failed to setLargeIcon", e);
}
NotificationCompat.BigPictureStyle s = new NotificationCompat.BigPictureStyle();
s.setSummaryText(Html.fromHtml(msg));
// expanded notification icon
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
if (expandedIconUrl != null) {
try {
s.bigLargeIcon(Picasso.with(context).load(expandedIconUrl).get());
} catch (IOException e) {
Log.e(this, "Failed to bigLargeIcon", e);
}
} else if (expandedIconResId > 0) {
s.bigLargeIcon(BitmapFactory.decodeResource(context.getResources(), expandedIconResId));
}
// background photo
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
try {
s.bigPicture(Picasso.with(context).load(bigImageUrl).get());
} catch (IOException e) {
Log.e(this, "Failed to bigPicture", e);
}
b.setStyle(s);
b.setContentIntent( /* add here your pending intent */ );
// here you can add up to 3 buttons to your notification
b.addAction(R.drawable.ic_notification_photo,
context.getString(R.string.notificationAction_viewPhoto),
/* and here the button onClick pending intent */);
Notification n = b.build();
// now just show it with Notify.发布于 2014-03-19 20:59:38
这就是我所做的:
public void createNotification() {
if (android.os.Build.VERSION.SDK_INT >= 11) {
createNotificationAPI11();
} else {
createNotificationOtherAPI();
}
}
private void createNotificationOtherAPI() {
// normal old notification
...
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void createNotificationAPI11() {
// new compact notification
....
}https://stackoverflow.com/questions/22119374
复制相似问题