安卓解决6.0以后没有setLatestEventInfo方法的解决方案

在将安卓sdk升级到6.0以后,发现没有setLatestEventInfo方法了,这样我们更新通知栏,创建通知栏就不兼容以前的类,官方给出的方法是使用Notification.Builder的方式替代以前的setLatestEventInfo方法,但是我们查看api发现Builder方法是API16以后才有的,如果有人用4.0或者2.3系统,我们的app就会找不到方法,略显蛋疼啊

解决方法如下

			if (Build.VERSION.SDK_INT <16) {
				 Class clazz = mNotification.getClass();
				 try {
					 Method m2 = clazz.getDeclaredMethod("setLatestEventInfo", Context.class,CharSequence.class,CharSequence.class,PendingIntent.class);
				        m2.invoke(mNotification, mContext, mContentTitle,
				        		contentText, mContentIntent); 
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
			        
//			        mNotification.setLatestEventInfo(mContext, mContentTitle,
//						mContentTitle, mContentIntent);
			}
			else
			{
				 mNotification = new Notification.Builder(mContext)    
		         .setAutoCancel(true)    
		         .setContentTitle(mContentTitle)    
		         .setContentText(contentText)    
		         .setContentIntent(mContentIntent)    
		         .setSmallIcon(icon)    
		         .setWhen(System.currentTimeMillis())    
		         .build();
			}

在api16以前我们通过反射调用setLatestEvenInfo,之后调用builder即可,这样既兼容了高版本,也兼容了低版本

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值