为什么我在通知中收到弃用警告..?

时间:2013-01-15 05:38:29

标签: android notifications

NotificationManager nm=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification n=new Notification(android.R.drawable.stat_notify_more , "My Alarm Ringing", System.currentTimeMillis());
Context cn=MainActivity.this;
CharSequence title="My Alarm Clock";
CharSequence details="Alarm Ringing....!!!";
Intent in=new Intent(cn,Alarm.class);
PendingIntent p=PendingIntent.getActivity(cn, 0, in, 0);
n.setLatestEventInfo(cn,title,details,p);
nm.notify(0,n);

在ecllipse中,我在第二行获得通知,而最后一行中的setLatestEventInfo被删除。为什么会这样..? 任何人都可以澄清什么是错误..? thanx的帮助

4 个答案:

答案 0 :(得分:3)

Deprecation

  

应用于功能,特征或实践的状态,表明应该避免这些状态,通常是因为它们已被取代。

警告会提醒您目标SDK中已弃用的方法,以便您可以尝试避免使用它。

在此特定情况下,警告建议您改为使用Notification.Builder,但如果您的需求不允许您使用Notification.Builder,则由于向后兼容性或其他原因,您可以(很可能) )继续使用setLatestEventInfo没有问题。这似乎只是对API的升级,而不是您需要避免的特别重要的事情。

答案 1 :(得分:2)

Notification.setLatestEventInfo()在API 11(source)中被删除。正如其他人所提到的,你应该使用Notification.Builder。 (source

如果您使用的是API 11之前的API,则可以使用Android提供的兼容包。使用此库将允许您对运行API 11及更低版本的设备使用API​​ 11及更高版本。 Android Support Library

另外,您知道,而不是使用Notification.Builder(),您需要使用NotificationCompat.Builder()。

答案 2 :(得分:1)

在你提到的目标SDK中,不推荐使用此方法...简单。

最好先阅读文档docs

答案 3 :(得分:0)

根据https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-notifications,"此版本删除了Notification.setLatestEventInfo()方法。使用Notification.Builder类来构造通知。"使用API​​ Level 23 for Android 6.0,当我尝试使用setLatestEventInfo()时,我的代码甚至无法编译。我收到以下错误:

Error:(86, 15) error: cannot find symbol method setLatestEventInfo(Context,String,String,PendingIntent)

您说您使用的是API级别17.如果您尝试使用API​​级别23编译代码,我只能确认您不能使用setLatestEventInfo()。

相关问题