/***
* 顯示指定通知
*
* @param strTitle
* 通知內容
* @param notification_id
* 通知id
*/
public void showNotification(String strText, int notification_id) {
// 得到NotificationManager
Log.i("ANYCHAT", "showNotification");
NotificationManager notificationManager = (NotificationManager) getSystemService(android.content.Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, strText, System.currentTimeMillis());
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.flags |= Notification.FLAG_AUTO_CANCEL; //通知被點擊後,自動消失
notification.defaults = Notification.DEFAULT_LIGHTS;
notification.ledARGB = Color.BLUE;
notification.ledOnMS = 100;
notification.ledOffMS = 100;
Intent notificationIntent = new Intent(BussinessCenter.mContext, BussinessCenter.mContext.getClass());
notificationIntent.putExtra("action", 2);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent contentIntent = PendingIntent.getActivity(
BussinessCenter.mContext, 0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(this,this.getString(R.string.BACKING_RUNING), strText,contentIntent);
notificationManager.notify(notification_id, notification);
}
在低版本中的代碼:(來自於AnyChatCallCenter中BcakService類的裏面),
但是,在版本里面已經被棄用,甚至hide起來了。
低於API Level 11版本,也就是Android 2.3.3以下的系統中,setLatestEventInfo()函數是唯一的實現方法。前面的有關屬性設置這裏就不再提了,網上資料很多。
在高版本中,可以用Builder和build()函數來配套的方便使用notification了。
Notification notification = new Notification.Builder(context)
.setAutoCancel(true)
.setContentTitle("title")
.setContentText("describe")
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.build();
參考網文:
[Android下setLatestEventInfo警告、Handler警告、SimpleDateFormat警告]{http://www.piaoyi.org/mobile-app/Android-setLatestEventInfo-Handler-SimpleDateFormat.html}