Android之Notification初识
生活随笔
收集整理的這篇文章主要介紹了
Android之Notification初识
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.Notification創建
? ?首先,介紹一下,創建一個通知所需要用到的類和方法? ?
?NotificationManager類
?NotificationManager類是用來管理系統的所有通知的類,該類的對象必須通過Context類的getSystemService()方法獲取。完整代碼:
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);? notify()作用是告知系統顯示該通知,有notify (int id, Notification notification)和notify (String tag,int id, Notification
? notification),id表示通知的id,tag表示通知的標志,主要用于區分各個通知,notification指的是通知對象;
?cancel(int id) 表示移除指定id的通知,cancel(String tag,int id)移除指定Id和tag的通知,cancelAll()移除所有通知。
? Notification類
? ?notification有一些常用的屬性:
? ? icon 設置通知圖標(在API23后使用setSmallIcon(Icon)替代)
? ? number 通知所顯示的事件數量,例如,收到郵件通知,則指的是郵件未讀數量(這是用API11創建的通知所表現的作用)。如果通知是用Notification.builder創建,則number表示擴展通知視圖,為0或者負數的時候,通知不顯示。
? ?tickerText 通知顯示在通知欄的文本,只在通知欄上顯示一次。
? ?when 系統當前時間
? ?flags 取值有:
? ? ? ? ? ? ? ? FLAG_AUTO_CANCEL 該通知能被狀態欄的清除按鈕給清除掉
FLAG_NO_CLEAR 該通知不能被狀態欄的清除按鈕給清除掉
FLAG_ONGOING_EVENT 通知放置在正在運行
FLAG_INSISTENT 是否一直進行,比如音樂一直播放,知道用戶響應
? ?
? ??defaults 設置默認值
DEFAULT_ALL 使用所有默認值,比如聲音,震動,閃屏等等
DEFAULT_LIGHTS 使用默認閃光提示
DEFAULT_SOUND 使用默認提示聲音
DEFAULT_VIBRATE 使用默認手機震動
? contentView 拉下通知欄后,通知條顯示視圖,類型是RemoteView;
?contentIntent 點擊通知條控件時,響應的意圖
? ?一些常用的方法
? ? 構造方法:
? ? ?public?Notification?(int icon,?CharSequence?tickerText, long when),如果使用屬性的方式設置這些值,那也可以使用無參構造函數 ?? ? 在API11之后使用Notification.builder()創建
? ?setLatestEventInfo(Context context,CharSequence title, CharSequence content, PendingIntent intent); ? ? ? ? ? ? ?本方法用于顯示通知欄下拉后,通知條的內容。
?PendingIntent類
PendingIntent這個類用于處理即將發生的事情。
該對象的獲取方式為,?PendingIntent.getActivity(Context context,int requestCode,Intent intent,int flags);requsetCode和flags一般默認設置為0;
下面用上面提到的知識,寫一個簡單的通知(基于API11之前):
public void showBaseNotification() {NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);Notification notify = new Notification();notify.icon = R.drawable.ic_launcher;notify.tickerText = "您有新短消息,請注意查收!";notify.when = System.currentTimeMillis();PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,new Intent(this, MainActivity.class), 0);notify.setLatestEventInfo(this, "Notification Title","This is the notification message", pendingIntent);notify.number = 1;notify.flags |= Notification.FLAG_AUTO_CANCEL; // 通過通知管理器來發起通知。如果id不同,則每click,在statu那里增加一個提示manager.notify(1, notify);}
?基于API11之后:
public void showNotification() {NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);NotificationCompat.Builder nb = new NotificationCompat.Builder(getApplicationContext()).setContentIntent(PendingIntent.getActivity(MainActivity.this, 0,new Intent(this, MainActivity.class),PendingIntent.FLAG_UPDATE_CURRENT)).setAutoCancel(true).setContentTitle("test title").setContentText("message").setSmallIcon(R.drawable.ic_launcher).setLights(Color.RED, 600, 1000).setVibrate(new long[] { 0, 200, 300, 500 }).setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));manager.notify(1, nb.build());}
總結
以上是生活随笔為你收集整理的Android之Notification初识的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mdt 计算机名_MDT配置数据库
- 下一篇: Eclipse里git提交冲突rejec