【问题标题】:Notification throws error when using vector drawables使用矢量可绘制对象时通知会引发错误
【发布时间】:2016-05-19 21:02:00
【问题描述】:

当我使用矢量可绘制对象设置通知的小图标时,出现以下异常:

android.app.RemoteServiceException:从包 com.qbes.xxx 发布的错误通知:无法创建图标:StatusBarIcon(pkg=com.qbes.xxxuser=0 id=0x7f020082 level=0 visible=true num=0 )

这是我的代码:

mNotificationBuilder = new android.support.v4.app.NotificationCompat.Builder(this)
                .setDefaults(android.support.v4.app.NotificationCompat.DEFAULT_LIGHTS)
                .setSound(null)
                .setSmallIcon(R.drawable.logo_white)
                .setColor(getResources().getColor(R.color.colorPrimary))
                .setCategory(android.support.v4.app.NotificationCompat.CATEGORY_PROGRESS)
                .setContentTitle("Trip in Progress...")
                .setAutoCancel(false)
                .setProgress(0, 0, progress)
                .setOngoing(true)
                .setPriority(android.support.v4.app.NotificationCompat.PRIORITY_MAX)
                .setOnlyAlertOnce(true)
                .setContentIntent(pendingIntent);

mNotificationBuilder.setContentText(body);

mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification note = mNotificationBuilder.build();

mNotificationManager.notify(Constants.NOTIFICATION_ID_Dash, note);

还有我的build.gradle(仅相关部分):

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.qbes.xxx"
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 720
        versionName "0.7.20"
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:design:23.2.1'
}

PS:当我使用 png 或 jpg 图像可绘制时,代码可以正常工作,但在使用矢量可绘制时会中断。

我已经搜索了一整天,但找不到任何对我有用的东西。任何想法。

【问题讨论】:

  • 您在哪个版本的 Android 上进行测试?您通常使用支持库 backport,但我认为这不适用于 Notification,因为该平台是尝试使用该资源的平台。
  • 我正在 API 19 上进行测试。我会检查其他版本是否存在问题。
  • 我已经检查了sdk版本,并避免为api版本21 Kitkat之前的版本调用setSmallIcon

标签: java android android-support-library android-notifications android-appcompat


【解决方案1】:

您正在使用矢量可绘制支持包。这很好,但这仅适用于您的应用程序。在 API 级别 21 之前,平台不知道如何使用矢量可绘制对象,对于 Notification,平台是渲染资源的平台。

您可以自己将可绘制矢量渲染到由Bitmap 支持的Canvas,然后在Notification 中使用该Bitmap。或者,您通常可以使用矢量反向移植库,但对于少数Notification 图标,为它们生成PNG 文件并在旧设备上使用它们。 Put the corresponding vector drawables in res/drawable-anydpi-v21/,较新的设备将使用矢量可绘制对象,而较旧的设备将使用 PNG。

【讨论】:

  • 还有一件事。小图标的首选 dp 大小是多少?
  • @ShahiM: 24dp,我想。
  • 为什么在旧设备上有 png 时再次使用矢量?!
  • 启动器图标也一样吗?
  • @SeshuVinay:是的,因为您不能假设主屏幕实现知道如何使用矢量可绘制对象。我尝试了这个并遇到了各种问题。
猜你喜欢
  • 1970-01-01
  • 2023-02-22
  • 2018-09-07
  • 1970-01-01
  • 2016-11-04
  • 2019-12-18
  • 2016-05-25
  • 2020-10-26
  • 2018-03-23
相关资源
最近更新 更多