Android 如果在用户设备上可用,如何显示压缩通知?

Android 如果在用户设备上可用,如何显示压缩通知?,android,android-notifications,Android,Android Notifications,我正在尝试显示这样一个紧凑的通知(扩展和崩溃的通知): 我收到以下错误: 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) 首先,如何检查当前设备上是否有

我正在尝试显示这样一个紧凑的通知(扩展和崩溃的通知):

我收到以下错误:

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的最大字符数是多少

这就是我所做的:

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
....
}

所以,这实际上取决于你想要使用的具体细节,但这里有一个我工作的应用程序的例子

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

该示例显示了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.

由于您的最小API为11,您应该使用
NotificationCompat
而不是
Notification
。NotificationCompat可以在支持库中找到确定我目前正在尝试是。使用支持库中的NotificationCompat,你可以去做任何事情,库会自动为你处理版本如果是这样,为什么你不给出解决方案作为答案,我似乎找不到它对不起,但即使你这样做,使用support library中的NotificationCompat可以避免代码重复两次。一次用于API>11,另一次用于API<11。我的意思是,谷歌的人已经写了这段代码,并把它放进了库中,就用它吧。我们写下正确的答案,这样我就可以把它标记为正确的答案。我正在使用你的代码,并试图在电池动作更改的接收器中显示通知。我收到错误01-08 17:54:49.497:E/AndroidRuntime(25734):java.lang.RuntimeException:在com.example.batterylevel.BatteryService中接收广播意图{act=android.Intent.action.BATTERY_CHANGED flg=0x60000010(具有额外功能)}时出错$1@4235d58801-08 17:54:49.497:E/AndroidRuntime(25734):在android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:773)01-08 17:54:49.497:E/AndroidRuntime(25734):位于android.os.Handler.handleCallback(Handler.java:733)
// 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.