【问题标题】:android notification large image not workingandroid通知大图像不起作用
【发布时间】:2013-06-04 12:39:42
【问题描述】:

我正在创建这样的通知:

        Intent intent = new Intent(this, OfferNotification.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0,
                intent, 0);
        Uri soundUri = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.unknown)
                //.setLargeIcon(BitmapFactory.decodeResource( getResources(), R.drawable.unknown))
                .addAction(R.drawable.ic_launcher, "d", pIntent)
                .setAutoCancel(true)
                .setContentTitle("Offer from " + restaurantName)
                .setContentText(offerDescriptoin).setSound(soundUri);
        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(this, OfferNotification.class);
        resultIntent.putExtra("offerID", offer.getID());
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

        stackBuilder.addParentStack(OfferNotification.class);

        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder
                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        mNotificationManager.notify(offer.getID(), mBuilder.build());

当我使用小图标时,效果很好,但是当我使用大图标时,我可以发出通知的声音但通知本身没有出现,请帮助?

【问题讨论】:

  • 如果您想在 11 API 中工作。
  • 我正在使用支持 v4 的 2.3,代码正常,只是大图标不起作用
  • 在支持 v4 中,不支持 NotificationCompat.Builder。你不能在 stackoverflow.com/a/8869647/1168654
  • 我认为是因为setLargeIcon方法被注释掉了。

标签: android android-notifications


【解决方案1】:

Android 图标(和其他 UI 元素,如拖动长度)以 dp 为单位。 dp 是与设备/密度无关的像素。 1 dp 相当于 160 dpi 屏幕上的 1 px。但要转换为其他屏幕密度,您需要将其乘以密度因子。因此,通常建议为大多数图标提供多张图片。

例如,状态栏中使用的通知图标被指定为 24x24 dp,具有 1 dp 边距(因此实际图标仅占用 22x22 dp 光学方块,尽管一些 AA 可以渗入该 1 dp边距/安全框)。要将 24 dp 转换为实际像素大小,需要使用以下粗略计算:

display density  dp units * scale = px units
ldpi  ~120 dpi   24x24 dp * .75   = 18x18 px
mdpi  ~160 dpi   24x24 dp * 1.0   = 24x24 px
hdpi  ~240 dpi   24x24 dp * 1.5   = 36x36 px
xhdpi ~320 dpi   24x24 dp * 2.0   = 48x48 px

还有一个称为 tvdpi (~213 dpi) 的中间显示密度,它位于 mdpi 和 hdpi 之间,比例因子为 1.33,但这并不常见。 Android 文档建议您在为最常见的显示密度提供预缩放位图图像(通常是 PNG)时遵循 3:4:6:8 的缩放比例。

我没有看到他们为通知中使用的大图标指定 dp 大小的地方,但在普通收件箱视图中每个通知的高度是 64 dp。这意味着显示的图标/图像的最大尺寸为:

ldpi:     48x48 px
mdpi:     64x64 px
hdpi:     96x96 px
xhdpi:  128x128 px

如果您想确切了解 Android 的股票图标的图像尺寸,您应该能够从 Android 图标模板包 v4.0 中找到。

【讨论】:

    【解决方案2】:

    我有同样的问题,因为我误解了对 SetSmallIcon 和 SetLargeIcon 的调用。 您必须指定一个小图标,否则不会显示通知。大图标是可选的,如果不设置,则使用小图标。

    【讨论】:

      【解决方案3】:

      我认为您应该在 Builder 中请求位图之前对其进行解码,如下所示:

       Bitmap bitmap = BitmapFactory.decodeResource( getResources(), R.drawable.unknown);
       Intent intent = new Intent(this, OfferNotification.class);
              PendingIntent pIntent = PendingIntent.getActivity(this, 0,
                      intent, 0);
              Uri soundUri = RingtoneManager
                      .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
              NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                      this).setSmallIcon(R.drawable.unknown)
                      //.setLargeIcon(bitmap)
                      .addAction(R.drawable.ic_launcher, "d", pIntent)
                      .setAutoCancel(true)
                      .setContentTitle("Offer from " + restaurantName)
                      .setContentText(offerDescriptoin).setSound(soundUri);
      

      它可能没有正确或及时解码。它也将在这里消除一个未知数。

      【讨论】:

      • 我试过你的解决方案,使用大图标时我仍然可以发出声音
      • 此代码在单线程(UI 循环器)上运行。没有比赛条件。好像是setLargeIcon方法被注释掉了。
      • 没什么区别
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多