【问题标题】:Issue with backward compatibility (setLatestEventInfo)向后兼容性问题 (setLatestEventInfo)
【发布时间】:2015-09-20 22:42:48
【问题描述】:

在 Android 6.0 (API 23) 中,方法 setLatestEventInfo(...) 已从类 android.app.Notification 中完全删除。我知道我现在必须使用Notification.Builder(context)....。新方法适用于较新的 Android 版本,但我仍想支持较旧的 Android 版本。

为了向后兼容,我现在将支持库 v4 添加到我的 gradle 文件中:

compile 'com.android.support:support-v4:23.0.1+'

不幸的是,我仍然无法使用 Android 6.0 的旧方法编译旧代码。

Error: cannot resolve method setLatestEventInfo(Context,CharSequence,CharSequence,PendingIntent)

我如何仍然使用旧方法来确保向后兼容到 Andoroid 2.3 (API 9)?

【问题讨论】:

    标签: android android-support-library


    【解决方案1】:

    新方式适用于较新的 Android 版本,但我仍想支持较旧的 Android 版本。

    android.support.v4.app.NotificationCompat.Builder,来自您加载的支持库,可返回 API 级别 4。

    不幸的是,我仍然无法使用 Android 6.0 的旧方法编译旧代码。

    正确。您应该从您加载的支持库中从 setLatestEventInfo() 转换为使用 android.support.v4.app.NotificationCompat.Builder

    具体来说,请致电 setContentTitle()setContentText()setContentIntent() 以复制您从 setLatestEventInfo() 获得的信息。

    或者,将您的 compileSdkVersion 降至 22 或更低。我强烈怀疑 setLatestEventInfo() 仍然存在,出于向后兼容性的原因,但它不再是 SDK 的一部分。

    【讨论】:

    • 完美!秘密是 android.support.v4.app.NotificationCompat.Builder 类。一旦我使用 NotificationCompat,一切都会完美。谢谢!
    【解决方案2】:

    NotificationCompat.Builder 的所有内容都与 Android 1.6 及更高版本的设备兼容 - 您应该使用它来支持所有 API 级别。

    【讨论】: