关于setLatestEventInfo(),Builder.getNotification()过时,Android developer提供的原文解决方案:
Notification(int icon, CharSequence tickerText, long when)
This constructor was deprecated in API level 11. Use Notification.Builder instead.
意思是该构造方法在API 11就开始不赞成使用,用Notification.Builder进行替代。
getNotification()
This method was deprecated in API level 16. Use build() instead.
意思是该构造方法在API 16就开始不赞成使用,用build()进行替代。
引用:解释
至此,我们得到了处理方案,下面是Android developer给出的一段demo:
Example:
Notification noti = new Notification.Builder(mContext)
.setContentTitle("New mail from " + sender.toString())
.setContentText(subject)
.setSmallIcon(R.drawable.new_mail)
.setLargeIcon(aBitmap)
.build();
我自己写了一段代码做对比:
API11:
private void testNotify() {
nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notification = new Notification();
notification.icon = R.drawable.ic_launcher;
notification.tickerText = "你也是通知";
notification.defaults = Notification.DEFAULT_SOUND;
intent = new Intent(this, JumpActivity.class);
pi = PendingIntent.getActivity(this, 0, intent, 0);// 用户点击该notification后才启动该activity
//API 11 这个方法过期
notification.setLatestEventInfo(this, "title22", "text33", pi);
nm.notify(1, notification);
}
API14:
private void testNotify1() {//建议使用这个API14
NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
// 新建一个Notification管理器;
// API level 11
Notification.Builder builder = new Notification.Builder(this);// 新建Notification.Builder对象
PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, JumpActivity.class), 0);
// PendingIntent点击通知后所跳转的页面
builder.setContentTitle("Bmob Test");
builder.setContentText("message");
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentIntent(intent);// 执行intent
//API 16时这个方法过期
Notification notification = builder.getNotification();// 将builder对象转换为普通的notification
notification.flags |= Notification.FLAG_AUTO_CANCEL;// 点击通知后通知消失
manager.notify(1, notification);// 运行notification
}
API16:
private void testNotify2() {
NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
// 新建一个Notification管理器;
PendingIntent intent2 = PendingIntent.getActivity(this, 0, new Intent(this, JumpActivity.class), 0);
Notification noti = new Notification.Builder(context)
.setContentTitle("New mail from ")
.setContentText("message")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(intent2)
.build();
//API level 16
noti.flags |= Notification.FLAG_AUTO_CANCEL;// 点击通知后通知消失
manager.notify(1, noti);// 运行notification
}