Android中关于setLatestEventInfo()过时以及构建Notification的解决方法

官方说法:
public Notification getNotification () Added in API level 11
This method was deprecated in API level 16. Use build() instead.
在API 11中,通过builder.getNotification()获取Notification对象已经过时了,替代方法是使用builder.builder();

Creating a Notification //创建一个Notification

You specify the UI information and actions for a notification in a NotificationCompat.Builder object. To create the notification itself, you call NotificationCompat.Builder.build(), which returns a Notification object containing your specifications. To issue the notification, you pass the Notification object to the system by calling NotificationManager.notify().(您可以在NotificationCompat中指定通知的UI信息和操作。构建器对象。创建通知本身,你叫NotificationCompat.Builder.build(),它返回一个通知对象包含您的规范。发出通知,通知对象传递给系统通过调用NotificationManager.notify())。

通过NotificationCompat.Builder.build()创建一个notification对象,它返回一个你已经创建的notification对象,通过使用 NotificationManager.notify()来进行系统通知



public Notification build ()
Added in API level 16 Combine all of the options that have been set and return a newNotification object.
Notification build ()是在API 16中才加入的,它包括了Notification所需要的所有选项,并返回一个notification对象

Required notification contents
A Notification object must contain the following:
A small icon, set by setSmallIcon()
A title, set by setContentTitle()
Detail text, set by setContentText()
一个Notification对象需要包括以下内容:
一个小图标:builder.setSmallIcon(R.drawable.ic_launcher);
一个标题:builder.setContentTitle("通知的标题");
一个内容:builder.setContentText("这是通知的内容");
具体如何创建:
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

Notification.Builder builder = new Notification.Builder(MainActivity.this);

builder.setContentTitle("通知的标题");

builder.setContentText("这是通知的内容");

builder.setSmallIcon(R.drawable.ic_launcher);

Notification notification = builder.build();

manager.notify(1, notification);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值