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);

In ecllipse i am getting Notification in second line and setLatestEventInfo in last second line is striked of. why is it so..? can anyone clearify what is the mistake..? thanx for the help

4

5 回答 5

3

Deprecation is

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

The warning alerts you to a deprecated method in your target SDK so that you can try to avoid using it.

In this specific context, the warning suggests that you use Notification.Builder instead, however if your needs do not allow you to use Notification.Builder, due to backwards compatability, or otherwise, you can (most likely) continue to use setLatestEventInfo without issue. It seems that this is just an upgrade to the API, not something particularly important that you need to avoid.

于 2013-01-15T05:41:06.080 回答
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;
}
于 2013-10-31T04:22:40.943 回答
2

Notification.setLatestEventInfo() 在 API 11 ( source ) 中已被弃用。正如其他人所提到的,您应该改用 Notification.Builder 。(来源

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

此外,正如您所知,您需要使用 NotificationCompat.Builder(),而不是使用 Notification.Builder()。

于 2013-06-13T00:39:26.350 回答
1

因为在您提到的目标 SDK 中,此方法已被弃用......简单。

最好阅读文档而不是文档

于 2013-01-15T05:41:36.563 回答
0

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

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

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

于 2017-03-09T16:43:50.657 回答