android notification 的总结分析
分类
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>初始化部分代码
publicclassMainActivityextendsActivityimplementsOnClickListener{privateint[]btns=newint[]{R.id.normal,R.id.inboxStyle,R.id.bigTextStyle,R.id.bigPicStyle,R.id.customize,R.id.progress,R.id.cancelNotification};privateNotificationManagermanager;privateBitmapicon=null;privatestaticfinalintNOTIFICATION_ID_NORMAL=1;privatestaticfinalintNOTIFICATION_ID_INBOX=2;privatestaticfinalintNOTIFICATION_ID_BIGTEXT=3;privatestaticfinalintNOTIFICATION_ID_BIGPIC=4;privatestaticfinalintNOTIFICATION_ID_CUSTOMIZE=5;privatestaticfinalintNOTIFICATION_ID_PROGRESS=6;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获取系统的通知服务manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);icon=BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);for(intbtn:btns){findViewById(btn).setOnClickListener(this);}}@OverridepublicvoidonClick(Viewv){switch(v.getId()){caseR.id.normal:showNormalNotification();break;caseR.id.inboxStyle:showInboxStyleNotification();break;caseR.id.bigTextStyle:showBigTextStyleNotification();break;caseR.id.bigPicStyle:showBigPicStyleNotification();break;caseR.id.customize:showCustomizeNotification();break;caseR.id.progress:showProgressBar();break;caseR.id.cancelNotification:cancelNotification();break;default:break;}}}
1>普通notification
privatevoidshowNormalNotification(){Notificationnotification=newNotificationCompat.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
privatevoidshowBigTextStyleNotification(){NotificationCompat.BigTextStyletextStyle=newNotificationCompat.BigTextStyle();textStyle.setBigContentTitle("BigContentTitle").setSummaryText("SummaryText").bigText("IamBigTexttttttttttttttttttttttttttttttttt"+"tttttttttttttttttttttttttttttttttttttttttttt"+"!!!!!!!!!!!!!!!!!!!......");Notificationnotification=newNotificationCompat.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
privatevoidshowInboxStyleNotification(){String[]lines=newString[]{"line1","line2","line3"};NotificationCompat.InboxStyleinboxStyle=newNotificationCompat.InboxStyle();inboxStyle.setBigContentTitle("BigContentTitle").setSummaryText("SummaryText");for(inti=0;i<lines.length;i++){inboxStyle.addLine(lines[i]);}Notificationnotification=newNotificationCompat.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
privatevoidshowBigPicStyleNotification(){NotificationCompat.BigPictureStylepictureStyle=newNotificationCompat.BigPictureStyle();pictureStyle.setBigContentTitle("BigContentTitle").setSummaryText("SummaryText").bigPicture(icon);Notificationnotification=newNotificationCompat.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
并对中间的播放按钮做了一个简单的点击处理事件:
privatevoidshowCustomizeNotification(){RemoteViewsremoteViews=newRemoteViews(getPackageName(),R.layout.custom_notification);Intentintent=newIntent(this,PlayMusicActivity.class);PendingIntentpendingIntent=PendingIntent.getBroadcast(this,0,intent,0);remoteViews.setOnClickPendingIntent(R.id.paly_pause_music,pendingIntent);NotificationCompat.Builderbuilder=newNotificationCompat.Builder(this);builder.setContent(remoteViews).setSmallIcon(R.drawable.ic_launcher).setLargeIcon(icon).setOngoing(true).setTicker("musicisplaying").setDefaults(Notification.DEFAULT_ALL);manager.notify(NOTIFICATION_ID_CUSTOMIZE,builder.build());}
布局文件:
<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns: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:
privatevoidshowProgressBar(){finalNotificationCompat.Builderbuilder=newNotificationCompat.Builder(this);builder.setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher).setTicker("showProgressBar").setContentInfo("ContentInfo").setOngoing(true).setContentTitle("Downloading...").setContentText("ContentText");newThread(newRunnable(){@Overridepublicvoidrun(){intprogress=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{//Sleepfor5secondsThread.sleep(2*1000);}catch(InterruptedExceptione){}}builder.setContentTitle("Downloadcomplete").setProgress(0,0,false).setOngoing(false);manager.notify(NOTIFICATION_ID_PROGRESS,builder.build());}}).start();}
更多相关文章
- Android布局管理器
- Android 弹出键盘向上顶布局
- Listview Section 多个标题以及内容
- ktolin在Android中布局界面拼接
- android之布局LinearLayout
- android之线性布局LinearLayout以及weight权重使用
- 【Android】使用代码动态创建布局
- Android Studio 自带的侧滑布局设置