android notification 的总结分析
為什么80%的碼農都做不了架構師?>>> ??
分類
?Android的notification有以下幾種:
1>普通notification
圖1
標題,通過NotificationCompat.Builder.setContentTitle(String title)來設置
大圖標,通過NotificationCompat.Builder.setLargeIcon(Bitmap icon)來設置
內容,通過NotificationCompat.Builder.setContentText("ContentText")來設置
內容附加信息,通過NotificationCompat.Builder.setContentInfo("ContentInfo")來設置
小圖標,通過NotificationCompat.Builder.setSmallIcon(int icon)來設置
時間,通過NotificationCompat.Builder.setWhen(when)來設置
注:
一個notification不必對上面所有的選項都進行設置,但有3項是必須的:
小圖標, set by?setSmallIcon()
標題, set by?setContentTitle()
內容, set by?setContentText()
2>大布局Notification
圖2
大布局notification是在android4.1以后才增加的,大布局notification與小布局notification只在‘7'部分有區別,其它部分都一致。大布局notification只有在所有notification的最上面時才會顯示大布局,其它情況下顯示小布局。你也可以用手指將其擴展為大布局(前提是它是大布局)。如下圖:
圖3
大布局notification有三種類型:如圖2為NotificationCompat.InboxStyle?類型。圖3左部為NotificationCompat.BigTextStyle。圖3右部 為:NotificationCompat.BigPictureStyle.
InboxStyle類型的notification看起來和BigTextStyle類型的notification,那么他們有什么不同呢?對于InboxStyle類型的notification,圖2的‘7’位置處每行都是很簡短的,第一行和最后兩行由于?內容很長,則使用了省略號略去了過長的內容?;而圖3的左圖中,BigTextStyle類型的notification則是將過長的內容分在了多行顯示。
3>自定義布局notification
除了系統提供的notification,我們也可以自定義notification。如下圖所示的一個音樂播放器控制notification:
圖4
創建自定義的notification
1>實例化一個NotificationCompat.Builder對象;如builder
2>調用builder的相關方法對notification進行上面提到的各種設置
3>調用builder.build()方法此方法返回一個notification對象。
4>獲取系統負責通知的NotificationManager;如:manager
5>調用manager的notify方法。
示例代碼
示例程序截圖:
圖5
0>初始化部分代碼
public?class?MainActivity?extends?Activity?implements?OnClickListener?{private?int[]?btns?=?new?int[]?{?R.id.normal,?R.id.inboxStyle,?R.id.bigTextStyle,?R.id.bigPicStyle,?R.id.customize,?R.id.progress,R.id.cancelNotification?};private?NotificationManager?manager;private?Bitmap?icon?=?null;private?static?final?int?NOTIFICATION_ID_NORMAL?=?1;private?static?final?int?NOTIFICATION_ID_INBOX?=?2;private?static?final?int?NOTIFICATION_ID_BIGTEXT?=?3;private?static?final?int?NOTIFICATION_ID_BIGPIC?=?4;private?static?final?int?NOTIFICATION_ID_CUSTOMIZE?=?5;private?static?final?int?NOTIFICATION_ID_PROGRESS?=?6;@Overrideprotected?void?onCreate(Bundle?savedInstanceState)?{super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//?獲取系統的通知服務manager?=?(NotificationManager)?getSystemService(NOTIFICATION_SERVICE);icon?=?BitmapFactory.decodeResource(getResources(),?R.drawable.ic_launcher);for?(int?btn?:?btns)?{findViewById(btn).setOnClickListener(this);}}@Overridepublic?void?onClick(View?v)?{switch?(v.getId())?{case?R.id.normal:showNormalNotification();break;case?R.id.inboxStyle:showInboxStyleNotification();break;case?R.id.bigTextStyle:showBigTextStyleNotification();break;case?R.id.bigPicStyle:showBigPicStyleNotification();break;case?R.id.customize:showCustomizeNotification();break;case?R.id.progress:showProgressBar();break;case?R.id.cancelNotification:cancelNotification();break;default:break;}} }1>普通notification
private?void?showNormalNotification()?{Notification?notification?=?new?NotificationCompat.Builder(this).setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher).setTicker("NormalNotification").setContentInfo("ContentInfo").setContentTitle("ContentTitle").setContentText("ContentText").setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL).build();manager.notify(NOTIFICATION_ID_NORMAL,?notification); }2>大布局Text類型notification
private?void?showBigTextStyleNotification()?{NotificationCompat.BigTextStyle?textStyle?=?new?NotificationCompat.BigTextStyle();textStyle.setBigContentTitle("BigContentTitle").setSummaryText("SummaryText").bigText("I?am?Big?Texttttttttttttttttttttttttttttttttt"+?"tttttttttttttttttttttttttttttttttttttttttttt"+?"!!!!!!!!!!!!!!!!!!!......");Notification?notification?=?new?NotificationCompat.Builder(this).setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher).setTicker("showBigTextStyleNotification").setContentInfo("contentInfo").setContentTitle("ContentTitle").setContentText("ContentText").setStyle(textStyle).setAutoCancel(false).setShowWhen(false).setDefaults(Notification.DEFAULT_ALL).build();manager.notify(NOTIFICATION_ID_BIGTEXT,?notification); }3> 大布局Inbox類型notification
private?void?showInboxStyleNotification()?{String[]?lines?=?new?String[]{"line1",?"line2",?"line3"};NotificationCompat.InboxStyle?inboxStyle?=?new?NotificationCompat.InboxStyle();inboxStyle.setBigContentTitle("BigContentTitle").setSummaryText("SummaryText");for?(int?i?=?0;?i?<?lines.length;?i++)?{inboxStyle.addLine(lines[i]);}Notification?notification?=?new?NotificationCompat.Builder(this).setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher).setTicker("showBigView_Inbox").setContentInfo("ContentInfo").setContentTitle("ContentTitle").setContentText("ContentText").setStyle(inboxStyle).setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL).build();manager.notify(NOTIFICATION_ID_INBOX,?notification); }4>大布局Picture類型notification
private?void?showBigPicStyleNotification()?{NotificationCompat.BigPictureStyle?pictureStyle?=?new?NotificationCompat.BigPictureStyle();pictureStyle.setBigContentTitle("BigContentTitle").setSummaryText("SummaryText").bigPicture(icon);Notification?notification?=?new?NotificationCompat.Builder(this).setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher).setTicker("showBigPicStyleNotification").setContentInfo("ContentInfo").setContentTitle("ContentTitle").setContentText("ContentText").setStyle(pictureStyle).setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL).build();manager.notify(NOTIFICATION_ID_BIGPIC,?notification); }5>自定義notification
效果圖:
圖6
并對中間的播放按鈕做了一個簡單的點擊處理事件:
private?void?showCustomizeNotification()?{RemoteViews?remoteViews?=?new?RemoteViews(getPackageName(),?R.layout.custom_notification);Intent?intent?=?new?Intent(this,?PlayMusicActivity.class);PendingIntent?pendingIntent?=?PendingIntent.getBroadcast(this,?0,?intent,?0);remoteViews.setOnClickPendingIntent(R.id.paly_pause_music,?pendingIntent);NotificationCompat.Builder?builder?=?new?NotificationCompat.Builder(this);builder.setContent(remoteViews).setSmallIcon(R.drawable.ic_launcher).setLargeIcon(icon).setOngoing(true).setTicker("music?is?playing").setDefaults(Notification.DEFAULT_ALL);manager.notify(NOTIFICATION_ID_CUSTOMIZE,?builder.build()); }布局文件:
<?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:gravity="center_vertical"android:orientation="horizontal"?><ImageViewandroid:id="@+id/singer_pic"android:layout_width="64dp"android:layout_height="64dp"android:src="@drawable/singer"?/><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:gravity="center_vertical"android:orientation="horizontal"?><ImageViewandroid:id="@+id/last_music"android:layout_width="0dp"android:layout_height="48dp"android:layout_weight="1"android:src="@drawable/player_previous"?/><ImageViewandroid:id="@+id/paly_pause_music"android:layout_width="0dp"android:layout_height="48dp"android:layout_weight="1"android:src="@drawable/player_pause"?/><ImageViewandroid:id="@+id/next_music"android:layout_width="0dp"android:layout_height="48dp"android:layout_weight="1"android:src="@drawable/player_next"?/></LinearLayout></LinearLayout>帶進度條的notification:
private?void?showProgressBar()?{final?NotificationCompat.Builder?builder?=?new?NotificationCompat.Builder(this);builder.setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher).setTicker("showProgressBar").setContentInfo("ContentInfo").setOngoing(true).setContentTitle("Downloading...").setContentText("ContentText");new?Thread(new?Runnable()?{@Overridepublic?void?run()?{int?progress?=?0;for?(progress?=?0;?progress?<?100;?progress?+=?5)?{//將setProgress的第三個參數設為true即可顯示為無明確進度的進度條樣式//builder.setProgress(100,?progress,?true);builder.setProgress(100,?progress,?false);manager.notify(NOTIFICATION_ID_PROGRESS,?builder.build());try?{//?Sleep?for?5?secondsThread.sleep(2?*?1000);}?catch?(InterruptedException?e)?{}}builder.setContentTitle("Download?complete").setProgress(0,?0,?false).setOngoing(false);manager.notify(NOTIFICATION_ID_PROGRESS,?builder.build());}}).start(); }
轉載于:https://my.oschina.net/itblog/blog/466334
總結
以上是生活随笔為你收集整理的android notification 的总结分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AD中父子域配置
- 下一篇: 怪物猎人世界刹现的荒天预感