[java] view plaincopy
- Notification n = new Notification(android.R.drawable.ic_menu_share, null, System.currentTimeMillis());
參數:表徵圖 ID,發送到狀態列瞬間的文字,目前時間
[java] view plaincopy
- PendingIntent contentIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
- n.setLatestEventInfo(this, "早上好!", "今天是個晴朗的天氣!", contentIntent);
3.發送到通知欄
[java] view plaincopy
- NotificationManager mNM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- mNM.notify(1001, n);
這樣就完成了一個通知的展示,很簡單!
[java] view plaincopy
- public void setLatestEventInfo(Context context,
- CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
- // TODO: rewrite this to use Builder
- RemoteViews contentView = new RemoteViews(context.getPackageName(),
- R.layout.notification_template_base);
- if (this.icon != 0) {
- contentView.setImageViewResource(R.id.icon, this.icon);
- }
- if (priority < PRIORITY_LOW) {
- contentView.setInt(R.id.icon,
- "setBackgroundResource", R.drawable.notification_template_icon_low_bg);
- contentView.setInt(R.id.status_bar_latest_event_content,
- "setBackgroundResource", R.drawable.notification_bg_low);
- }
- if (contentTitle != null) {
- contentView.setTextViewText(R.id.title, contentTitle);
- }
- if (contentText != null) {
- contentView.setTextViewText(R.id.text, contentText);
- }
- if (this.when != 0) {
- contentView.setViewVisibility(R.id.time, View.VISIBLE);
- contentView.setLong(R.id.time, "setTime", when);
- }
- if (this.number != 0) {
- NumberFormat f = NumberFormat.getIntegerInstance();
- contentView.setTextViewText(R.id.info, f.format(this.number));
- }
-
- this.contentView = contentView;
- this.contentIntent = contentIntent;
- }
可以看到,他實際上就是使用系統預設布局為我們建立了一個 RemoteViews ,
[java] view plaincopy
- contentView.setImageViewResource(R.id.icon, this.icon);
其實就是設定表徵圖了:
[java] view plaincopy
- RemoteViews.setImageViewBitmap(int viewId, Bitmap bitmap)
[java] view plaincopy
- Class<?> clazz = Class.forName("com.android.internal.R$id");
-
- Field field = clazz.getField("icon");
- field.setAccessible(true);
- id_icon = field.getInt(null);
[java] view plaincopy
- n.setLatestEventInfo(context, title, msg, contentIntent);
- n.flags |= Notification.FLAG_AUTO_CANCEL;
-
- if(n.contentView != null && icon != null){
- n.contentView.setImageViewBitmap(InternalID.id_icon, icon);
- }
發出通知,下拉通知欄看看,表徵圖是不是變了^_^