向后兼容性问题 (setLatestEventInfo)

Issue with backward compatibility (setLatestEventInfo)

在 Android 6.0 (API 23) 中,方法 setLatestEventInfo(...) 已从 class 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)

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

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

The new way works with newer Android versions but I still want to support older Android versions.

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

Unfortunately I still cannot compile the old code with the old method using Android 6.0.

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

具体来说,调用 setContentTitle()setContentText()setContentIntent() 来复制您从 setLatestEventInfo() 获得的内容。

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