在將安卓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即可,這樣既兼容了高版本,也兼容了低版本