日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android之Notification初识

發布時間:2023/12/10 Android 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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初识的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。