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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Notification详解

發布時間:2025/4/16 编程问答 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Notification详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 創建notification

Notification.Builder builder = new Notification.Builder(this).setSmallIcon(R.id.icon).setContentTitle("標題").setContentText("詳細文本");

通過Builder模式創建Notification.Builder實例,有了builder對象,可以給它添加各種屬性,例如標題,內容,圖標等

2. 定義Action

給點擊Notification后要執行的操作增加一個Intent,由于這個Intent不是馬上就執行的,而是有用戶觸發的,所以Android給這樣的Intent提供了一個延遲意圖PendingIntent來幫助我們完成這樣的操作

PendingIntent的使用非常簡單,只需要在Intent的基礎上包裝一層就可以了,代碼如下所示

Intent resultIntent = new Intent(this, ResultActivity.class); PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);

這樣當點擊Notification后,就會觸發PendingIntent事件,跳轉到指定的Activity

3. 設置點擊事件

builder.setContentIntent(resultPendingIntent);

注意事項:當點擊通知跳轉到Activity的時候,Activity會重新走生命周期,想要保持原來的狀態,需要給Activity配置一個launchMode = “singleTask”

4. 發送通知

通過NotificationManager通知管理器的notify()方法來發送Notification,并給Notification一個id值,這個id會在更新Notification的時候用到

NotificationManager mNotifyMgr =(NotificationManager) getSystemService(NOTIFICATION_SERVICE); int mNotificationId = 001; mNotifyMgr.notify(mNotificationId, builder.build());

5. 使用BigView樣式

Notification.Builder builder = new Notification.Builder(this).setSmallIcon(R.drawable.ic_stat_notification).setContentTitle(getString(R.string.notification)).setContentText(getString(R.string.ping)).setDefaults(Notification.DEFAULT_ALL).setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).addAction (R.drawable.ic_stat_dismiss,getString(R.string.dismiss), piDismiss).addAction (R.drawable.ic_stat_snooze,getString(R.string.snooze), piSnooze);

6. 顯示Notification進度

int id = 1; ... NotificationManager mNotifyManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification.Builder mBuilder = new Notification.Builder(this); mBuilder.setContentTitle("Picture Download").setContentText("Download in progress").setSmallIcon(R.drawable.ic_notification);new Thread(new Runnable() {@Overridepublic void run() {int i;for (i = 0; i <= 100; i+=5) {mBuilder.setProgress(100, i, false);mNotifyManager.notify(id, mBuilder.build());Thread.sleep(5*1000);}mBuilder.setContentText("Download complete").setProgress(0,0,false);mNotifyManager.notify(id, mBuilder.build());}} ).start();

7. 更新通知

根據id來更新通知

8. 自定義通知布局

Notification的自定義布局通過RemoteViews去實現,調用Notification.Builder的setCustomContentView()方法設置自定義的布局

//通過RemoteViews來創建自定義的Notification視圖 RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification); contentView.setTextViewText(R.id.tv, "show me when collapsed");Notification.Builder builder = new Notification.Builder(this).setCustomContentView(contentView);

折疊式Notification

折疊式Notification 也是一種自定義視圖的Notification ,常常用于顯示長文本。它擁有兩個視圖狀態, 一個是普通狀態下的視圖狀態, 另一個是展開狀態下的視圖狀態。在Notitication中,使用RemoteViews 來幫助我們創建一個自定義的Notification 視圖,代碼如下所示。

//通過RemoteViews來創建自定義的Notification視圖RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.notification);contentView.setTextViewText(R.id.tv,"show me when collapsed");

懸掛式Notification

觸發懸掛式的Notification的條件有

  • Activity處于全屏模式
  • Notification擁有很高的優先級
Notification.Builder builder = new Notification.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setPriority(Notification.PRIORITY_DEFAULT).setCategory(Notification.CATEGORY_MESSAGE).setContentTitle("Headsup Notification").setContentText("I am Headsup Notification");Intent push = new Intent(); push.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); push.setClass(this,MainActivity.class);PendingIntent pendingIntent = PendingIntent.getActivity(this,0,push,PendingIntent.FLAG_CANCEL_CURRENT);NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(1,builder.build());

Notification的顯示等級

Android 5.x 將Notification分成了三個等級

  • VISIBILITY_PRIVATE:表面只有當沒有鎖屏的時候才能夠顯示
  • VISIBILITY_PUBLIC:表明任何情況下都會顯示
  • VISIBILITY_SECRET:表明在pin,password等安全鎖和沒有鎖屏的情況下才能夠顯示

設置Notification的顯示等級

Notification.Builder builder = new Notification.Builder(this).setVisibility(Notification.VISIBILITY_PRIVATE);

9. 創建一個常規的activity延遲意圖

<activity android:name=".MainActivity"android:label="@string/app_name" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter> </activity> <activity android:name=".ResultActivity"android:parentActivityName=".MainActivity"><meta-data android:name="android.support.PARENT_ACTIVITY"android:value=".MainActivity"/> </activity>

為Intent創建一個回退棧

... Intent resultIntent = new Intent(this, ResultActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); // Adds the back stack stackBuilder.addParentStack(ResultActivity.class); // Adds the Intent to the top of the stack stackBuilder.addNextIntent(resultIntent); // Gets a PendingIntent containing the entire back stack PendingIntent resultPendingIntent =stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); ... NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setContentIntent(resultPendingIntent); NotificationManager mNotificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(id, builder.build());

10. 創建一個特別的activity延遲意圖

<activity android:name=".ResultActivity"...android:launchMode="singleTask"android:taskAffinity=""android:excludeFromRecents="true"> </activity> ...NotificationCompat.Builder builder = new NotificationCompat.Builder(this);Intent notifyIntent = new Intent(this, ResultActivity.class); notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK);PendingIntent notifyPendingIntent =PendingIntent.getActivity(this,0,notifyIntent,PendingIntent.FLAG_UPDATE_CURRENT);builder.setContentIntent(notifyPendingIntent);NotificationManager mNotificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);mNotificationManager.notify(id, builder.build());

11、Android 7.0 通知新特性

在Android N中重新設計了通知,可以達到更容易、更快使用的效果。一些主要的變化包括:

模板更新:更新了通知模板重點內容和頭像。開發者將能夠利用的新模板的優勢,在他們的代碼中實現最低限度的調整。

捆綁通知:Android N的通知功能也更加人性化,現在會自動將相同應用的通知捆綁在一起,實現分組顯示,并且通過兩指滑動實現預覽,理論上用戶可以在通知界面直接閱讀郵件等內容。

直接回復:對于實時通信應用程序,Android系統支持在線回復,使用戶可以以短信或短信通知界面內快速、直接響應。

自定義視圖:兩個新的 API 讓用戶在通知中使用自定義視圖。

Android N 開發者預覽版的通知系統中還加入了兩個全新的 API 接口:Direct Replies 和 Bundling。前者支持為第三方應用的通知加入快速回復和快捷操作,后者則允許同時發出多條通知的應用進行通知拆分。

當一款應用完美的適配了 Android N,當收到一條消息時就可以直接在下拉通知抽屜甚至是鎖屏中直接呼出輸入框進行回復,或是選擇事先設定好的快速處理操作(標記為已讀、轉發等)。而當用戶同時收到來自不同聯系人的消息時,可以點擊知卡片上的通知拆分按鈕對已經合并的通知進行拆分,拆分后的通知可以像其他的獨立通知一樣進行回復和處理。

當然,現階段適配了這兩個特性的應用屈指可數,除了 Google 的環聊、Messenger 以及 Gmail 等應用以外,目前僅發現第三方 Telegram 客戶端 Plus Messenger 支持以上功能。

面對各種應用的通知推送, Android N取以優先級為核心的通知管理方式,而在 Android N中,通知管理也變得更加簡單:只需在需要在相應的通知上左右輕掃便能看見一個設置圖標,點擊該圖標就能在通知上方呼出一個簡潔的通知優先級設定界面,在這個界面可以將應用通知設定為“靜默顯示”、“阻攔所有通知”和“默認”三個等級。

如果在”系統界面調諧器 - 其它“中打開了”Show full importance settings”功能,這三個等級又將變為”屏蔽 - 低 - 一般 - 高 - 緊急”5 個,設定的方式也由縱列選項變為左右滑動。這個看似新穎的設計實際上是對現有通知管理操作的一次簡化,在 Android 6.0 中需要在兩個界面來回跳轉才能完成的操作,在Android 7.0只用在一個界面就可以搞定。

同時,Google 也將其對通知優先級的定義從”幕后”搬到了”臺前”,在進行完整的五層次優先級設定時 Google 還會提醒不同優先級所對應的通知效果。最后,勿擾模式也在 Android N 中得到了完善,加入了自動規則并允許用戶在“請勿打擾”模式下屏蔽靜音通知的彈窗甚至是手機的通知指示燈。

發送一個通知

private int i = 0; private NotificationManager mNotificationManager; private static final String GROUP_NAME = "com.heima.notification_type";//[1]獲取一個NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); //[2]創建一個Notification //[2.1]需要給這個Notification對象指定icon,標題,內容, final Notification builder = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_notification).setContentTitle(getString(R.string.app_name)).setContentText("這是一個消息通知").setAutoCancel(true).setGroup(GROUP_NAME).build(); //[3]發送一個通知 需要指定一個Notification對象和一個id,這個id必須是唯一的 mNotificationManager.notify(i++, builder); updateNotificationSummary();

多個通知放入到一個組內

//[4]創建一個notification組 String notificationContent = "傳智播客" + i; final NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_notification).setStyle(new NotificationCompat.BigTextStyle().setSummaryText(notificationContent)).setGroup(GROUP_NAME).setGroupSummary(true); final Notification notification = builder.build(); //[5]發送這個notification組 mNotificationManager.notify(101, notification);

可以回復的通知

private NotificationManager mNotificationManager; //[1]獲取一個NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); //[2]創建remoteInput對象,這個對象指定了這個notification的標題和一個key String replyLabel = getResources().getString(R.string.app_name); RemoteInput remoteInput = new RemoteInput.Builder("heima").setLabel(replyLabel).build(); //[3]創建一個Action對象 可以指定用戶一個友好的輸入提示,可以指定跳轉意圖, Intent deleteIntent = new Intent(""); Notification.Action action =new Notification.Action.Builder(R.mipmap.ic_launcher,"請輸入內容", PendingIntent.getActivity(this, 10002, deleteIntent, 0)).addRemoteInput(remoteInput).build();//[3]創建一個Notification對象 Notification notification =new Notification.Builder(this).setSmallIcon(R.mipmap.ic_notification).setContentTitle("傳智播客").setContentText("消息通知").addAction(action).build();//[4]發送這個notification mNotificationManager.notify(1, notification); public class MainActivity extends Activity {private int i = 0;private NotificationManager mNotificationManager;private static final String GROUP_NAME = "com.heima.notification_type";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void sendNotification(View v){//[1]獲取一個NotificationManagermNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);//[2]創建一個Notification//[2.1]需要給這個Notification對象指定icon,標題,內容,final Notification builder = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_notification).setContentTitle(getString(R.string.app_name)).setContentText("這是一個消息通知").setAutoCancel(true).setGroup(GROUP_NAME).build();//[3]發送一個通知 需要指定一個Notification對象和一個id,這個id必須是唯一的mNotificationManager.notify(i++, builder);}public void sendGroup(View v){//[1]獲取一個NotificationManagermNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);//[2]創建一個Notification//[2.1]需要給這個Notification對象指定icon,標題,內容,final Notification builder = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_notification).setContentTitle(getString(R.string.app_name)).setContentText("這是一個消息通知").setAutoCancel(true).setGroup(GROUP_NAME).build();//[3]發送一個通知 需要指定一個Notification對象和一個id,這個id必須是唯一的mNotificationManager.notify(i++, builder);//[4]創建一個notification組String notificationContent = "傳智播客" + i;final NotificationCompat.Builder builder1 = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_notification).setStyle(new NotificationCompat.BigTextStyle().setSummaryText(notificationContent)).setGroup(GROUP_NAME).setGroupSummary(true);final Notification notification = builder1.build();//[5]發送這個notification組mNotificationManager.notify(101, notification);}public void send(View v){//[1]獲取一個NotificationManagermNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);//[2]創建remoteInput對象,這個對象指定了這個notification的標題和一個keyString replyLabel = getResources().getString(R.string.app_name);RemoteInput remoteInput = new RemoteInput.Builder("heima").setLabel(replyLabel).build();//[3]創建一個Action對象 可以指定用戶一個友好的輸入提示,可以指定跳轉意圖,Intent deleteIntent = new Intent(this,MainActivity.class);Notification.Action action =new Notification.Action.Builder(R.mipmap.ic_launcher,"請輸入內容", PendingIntent.getActivity(this, 10002, deleteIntent, 0)).addRemoteInput(remoteInput).build();//[3]創建一個Notification對象Notification notification =new Notification.Builder(this).setSmallIcon(R.mipmap.ic_notification).setContentTitle("傳智播客").setContentText("消息通知").addAction(action).build();//[4]發送這個notificationmNotificationManager.notify(1, notification);} }

http://android.xsoftlab.net/guide/topics/ui/notifiers/notifications.html#CustomNotification

http://android.xsoftlab.net/design/patterns/notifications.html

自定義Notification

布局文件

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextView android:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="自定義的notification" /><ProgressBar android:id="@+id/progressBar1"android:max="100"android:progress="50"style="?android:attr/progressBarStyleHorizontal"android:layout_width="fill_parent"android:layout_height="wrap_content" /></LinearLayout>

實現代碼

public class DemoActivity extends Activity {/ Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);}public void click(View view){System.out.println("haha");//1.創建notification 的管理器NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);//2 .創建notification的實例Notification notification = new Notification();notification.flags = Notification.FLAG_AUTO_CANCEL;notification.icon = R.drawable.ic_launcher;notification.tickerText="自定義notification";//notification.contentView;//遠程的view 我們的view對象 是要顯示在另外一個進程里面 另外一個程序里面 所以就需要一個remote viewRemoteViews rv = new RemoteViews(getPackageName(), R.layout.custom_notification);rv.setTextViewText(R.id.textView1, "textview 自定義notification");rv.setProgressBar(R.id.progressBar1, 100, 50, false);notification.contentView = rv;Intent intent = new Intent(this,DemoActivity.class);PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);notification.contentIntent = pendingIntent;manager.notify(2, notification);} }

總結

以上是生活随笔為你收集整理的Notification详解的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 九色视频91 | 人妻少妇无码精品视频区 | 韩日精品视频 | 色呦呦在线观看视频 | 亚洲精品国产精华液 | 蝌蚪自拍网站 | jizz黄色片| 毛片成人网 | 男男gay同性三级 | 麻豆传媒网页 | 久久久久女教师免费一区 | 欧美涩涩涩 | 久久77777 | 日韩毛片 | 播放男人添女人下边视频 | 婷婷九月| 国产精品久久久久影院色老大 | 成人天堂网 | 美女又爽又黄免费视频 | 精品国产一区二区三区性色 | 一个人在线免费观看www | 精品国产aⅴ | 亚洲黄色av网站 | 久久视频精品 | 欧美黑人添添高潮a片www | 成人观看视频 | 久久久精品免费视频 | 国产污片在线观看 | 熟妇大屁股一区二区三区视频 | 黄色网页网站 | 国产一区在线观看视频 | 欧美日韩精品久久 | 色网站免费在线观看 | 最新成人| av不卡高清| 久久精品久久久久 | 国产欧美久久久 | 黄a免费网络 | 国产日韩一区二区三免费高清 | 日日操日日碰 | 亚洲aaaaaa | 就要操就要日 | 国产成人无码精品久久久久久 | 刘亦菲国产毛片bd | 亚洲区视频在线观看 | 人妻内射一区二区在线视频 | 天天躁夜夜操 | 亚洲最大在线观看 | 麻豆影视在线 | 嫩草私人影院 | 巨胸喷奶水www久久久免费动漫 | 亚洲综合射| 羞辱狗奴的句子有哪些 | 少妇太爽了 | 免费欧美一级 | 男人天堂aaa | 潘金莲一级淫片aaaaa | 亚洲国产日韩一区无码精品久久久 | 干美女视频 | 免费在线一区二区三区 | 欧美性大战久久久久久久蜜桃 | 日产亚洲一区二区三区 | 日韩午夜毛片 | 99色婷婷| 粉嫩aⅴ一区二区三区 | 91嫩草在线 | 91国产大片 | 国产精品久久久久9999爆乳 | 麻豆69xxnxxporn | 国产精品久久久久久久久久久久久久久久久久 | 丁香婷婷久久 | 日韩欧美的一区二区 | 日韩欧美一区二区三区在线 | 羞辱狗奴的句子有哪些 | 原创真实夫妻啪啪av | 在线91观看 | 成人黄色av网站 | 日本女人性视频 | 欧美亚洲 | 综合图区亚洲 | 成人日皮视频 | 午夜视频免费看 | 久久精品国产亚洲7777 | 免费看成人av | 国产在线视频在线观看 | 悠悠色在线| 成人免费在线视频网站 | 色亚洲视频 | 日韩欧美国产电影 | 亚洲自拍p| 91精品国产一区二区三区蜜臀 | 你懂的在线播放 | 日本成人免费在线 | 99国产精品白浆在线观看免费 | 狼人伊人干 | 91.久久| 羞羞软件| 成年人在线播放视频 | www插插插无码免费视频网站 |