2013-01-15 56 views
3
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的帮助为什么我在通知中得到弃用警告..?

+0

什么API来编译你的代码不能使用setLatestEventInfo()你在用吗? – 2013-01-15 05:40:55

+1

请参阅[here](http://developer.android.com/reference/android/app/Notification.html)'setLatestEventInfo方法在API级别11中已弃用。使用Notification.Builder替代' –

+0

即时使用API​​级别17 – Scorpian

回答

3

Deprecation

a status applied to features, characteristics, or practices to indicate that they should be avoided, typically because they have been superseded.

的警告提醒你在你的目标SDK已经被废弃的方法,这样就可以尽量避免使用它。

在这种特定的情况下,警告建议您使用Notification.Builder代替,但是如果你的需求不允许你使用Notification.Builder,由于向后兼容,否则,您可以(最有可能)继续使用setLatestEventInfo没有问题。看起来,这只是对API的升级,而不是您需要避免的特别重要的事情。

+1

我该如何删除它..? – Scorpian

+0

改为使用'Notification.Builder'。 –

+1

@ dicarlo2 ..不可能,如果他正在使用** minSdk 8 ** ..他可以将目标sdk设置为8而不是...... – ngesh

1

由于您在目标SDK中提到过,此方法已被弃用...简单。

其更好地阅读文档,而不是docs

2

Notification.setLatestEventInfo()API中的11(source)的depreceated。正如其他人所提到的,您应该使用Notification.Builder代替。 (source

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

此外,只是你知道,而不是使用Notification.Builder()你需要使用NotificationCompat.Builder()。

2

对旧版本使用NotificationCompat。

private NotificationCompat.Builder buildNormal() { 
    NotificationCompat.Builder b = new NotificationCompat.Builder(mContext.getApplicationContext()); 

    b.setAutoCancel(true) 
    .setDefaults(Notification.DEFAULT_ALL) 
    .setWhen(System.currentTimeMillis())   
    .setSmallIcon(R.drawable.ic_launcher) 
    .setTicker("Optional ticker")    
    .setContentTitle("Default notification") 
    .setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.") 
    .setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_VIBRATE| Notification.DEFAULT_SOUND) 
    .setContentIntent(buildPendingIntent()) 
    .setContentInfo("Info"); 


    return b; 
} 
0

根据https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-notifications “这释放移除Notification.setLatestEventInfo()方法,使用Notification.Builder类,而不是构造通知。”对于Android 6.0使用API​​ Level 23,当我尝试使用setLatestEventInfo()时,我的代码甚至无法编译。我得到以下错误:

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

你说你使用API​​级别17.我只能确定,如果你想与API级别23

+0

我在http://stackoverflow.com/questions/19474116/the-constructor-notification-is-deprecated上阅读,“Notification的构造函数在API 11以前不推荐使用” 。我们现在应该使用这个:https://developer.android.com/reference/android/app/Notification.Builder.html。 –

相关问题