在将安卓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即可,这样既兼容了高版本,也兼容了低版本