Notification中setLatestEventInfo(),Builder.getNotification()过时

关于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

	}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值