首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果在用户设备上可用,如何显示紧凑的通知?

如果在用户设备上可用,如何显示紧凑的通知?
EN

Stack Overflow用户
提问于 2014-03-01 19:40:48
回答 2查看 1.6K关注 0票数 0

我试图显示类似这样的紧凑通知(展开和折叠的通知):

代码语言:javascript
复制
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();

我收到以下错误:

代码语言:javascript
复制
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?

第三,构建后如何实际触发通知出现?它和老的一样吗?

代码语言:javascript
复制
// show the notification
mNotificationManager.notify(1, noti);

第四,bigText的最大字符数是多少?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-19 21:49:32

因此,这确实取决于您想要使用的具体细节,但是这里是我工作的应用程序中的一个示例。

请注意,我只尝试加载图像,如果它将被使用,否则,我浪费内存和下载时间。

该示例显示了BigPicture样式,但您也可以从文档中获得收件箱或BigText样式。

代码语言:javascript
复制
// 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.
票数 2
EN

Stack Overflow用户

发布于 2014-03-19 20:59:38

这就是我所做的:

代码语言:javascript
复制
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
....
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22119374

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档