Android Notification 的总结分析

原作地址:http://www.jb51.net/article/36567.htm

在原作基础上做了修改和补充,更完善些。

notification有以下3种 ###############################################################

1>普通notification


1.内容标题

2.大图标

3.内容

4.内容附加信息

5.小图标

6.时间

2>大布局Notification


                                         图1

大布局notification是在android4.1以后才增加的,大布局notification与小布局notification只在‘7'部分有区别,其它部分都一致。大布局notification只有在所有notification的最上  面时才会显示大布局,其它情况下显示小布局。你也可以用手指将其扩展为大布局(前提是它是大布局)。如下图:

              

                                                              图2

大布局notification有三种类型

图1为NotificationCompat.InboxStyle 类型。

图2左部为NotificationCompat.BigTextStyle。

图2右部 为:NotificationCompat.BigPictureStyle

3>自定义布局notification

除了系统提供的notification,我们也可以自定义notification。如下图所示的一个音乐播放器控制notification:

    

                                      图3

如何创建notification ###############################################################

1>实例化一个NotificationCompat.Builder对象;如builder

2>调用builder的相关方法对notification进行上面提到的各种设置

3>调用builder.build()方法此方法返回一个notification对象。

4>实例化一个NotificationManager对象;如:manager

5>调用manager的notify方法。

不同版本的开发平台的语法有些不一样,注意看下面的写法:

  
  
  1. // 低于 API 11 写法
  2. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
  3. notification = new Notification(R.mipmap.ic_launcher, "有通知到来", System.currentTimeMillis());
  4. notification.setLatestEventInfo(this, "这是通知的标题", "这是通知的内容", pendingIntent);
  5. } if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN){
  6. // 高于 API 11 低于 API 16
  7. Notification.Builder builder = new Notification.Builder(this)
  8. .setAutoCancel(true)
  9. .setContentTitle("有通知到来")
  10. .setContentText("describe")
  11. .setContentIntent(pendingIntent)
  12. .setSmallIcon(R.mipmap.ic_launcher)
  13. .setWhen(System.currentTimeMillis())
  14. .setContent(remoteViews)
  15. .setOngoing(true);
  16. notification = builder.getNotification();
  17. } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
  18. // 高于 API 16
  19. notification = new Notification.Builder(this)
  20. .setAutoCancel(true)
  21. .setContentTitle("有通知到来")
  22. .setContentText("describe")
  23. .setContentIntent(pendingIntent)
  24. .setSmallIcon(R.mipmap.ic_launcher)
  25. .setWhen(System.currentTimeMillis())
  26. .setOngoing(true)
  27. .setContent(remoteViews)
  28. .build();
  29. }
  30. // notification.defaults = Notification.DEFAULT_SOUND;// 默认响玲
  31. // notification.flags = Notification.FLAG_NO_CLEAR;//设置通知点击或滑动时不被清除
  32. // 在Service里调用startForeground()方法就可以让Service变成一个前台Service
  33. // startForeground(notificationId, notification);
  34. //获取通知管理器
  35. NotificationManager mnotiManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  36. //发送通知
  37. mnotiManager.notify(notificationId, notification);

注:一个notification不必对上面所有的选项都进行设置,但有3项是必须的:

小图标 setSmallIcon()

内容标题 setContentTitle()

内容 setContentText()


3种notification写法例子 ################################################################

1>普通notification

  
  
  1. private void showNormal() {
  2. Notification notification = new NotificationCompat.Builder(context)
  3. .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
  4. .setTicker("showNormal").setContentInfo("contentInfo")
  5. .setContentTitle("ContentTitle").setContentText("ContentText")
  6. .setNumber(++messageNum)
  7. .setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
  8. .build();
  9. manager.notify(NOTIFICATION_ID_1, notification);
  10. }

2>大布局Text类型notification

  
  
  1. private void showBigView_Text() {
  2. NotificationCompat.BigTextStyle textStyle = new BigTextStyle();
  3. textStyle.setBigContentTitle("BigContentTitle")
  4. .setSummaryText("SummaryText")
  5. .bigText("I am Big Texttttttttttttttttttttttttttttttttttttttttttt!!!!!!!!!!!!!!!!!!!......");
  6. Notification notification = new NotificationCompat.Builder(context)
  7. .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
  8. .setTicker("showBigView_Text").setContentInfo("contentInfo")
  9. .setContentTitle("ContentTitle").setContentText("ContentText")
  10. .setStyle(textStyle)
  11. .setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
  12. .build();
  13. manager.notify(NOTIFICATION_ID_2, notification);
  14. }

3> 大布局Picture类型notification

  
  
  1. private void showBigView_Pic() {
  2. NotificationCompat.BigPictureStyle pictureStyle = new BigPictureStyle();
  3. pictureStyle.setBigContentTitle("BigContentTitle")
  4. .setSummaryText("SummaryText").bigPicture(icon);
  5. Notification notification = new NotificationCompat.Builder(context)
  6. .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
  7. .setTicker("showBigView_Pic").setContentInfo("contentInfo")
  8. .setContentTitle("ContentTitle").setContentText("ContentText")
  9. .setStyle(pictureStyle)
  10. .setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
  11. .build();
  12. manager.notify(NOTIFICATION_ID_3, notification);
  13. }

4>大布局Inbox类型notification

  
  
  1. private void showBigView_Inbox() {
  2. NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
  3. inboxStyle.setBigContentTitle("BigContentTitle").setSummaryText(
  4. "SummaryText");
  5. for (int i = 0; i < 5; i++)
  6. inboxStyle.addLine("news:" + i);
  7. Notification notification = new NotificationCompat.Builder(context)
  8. .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
  9. .setTicker("showBigView_Inbox").setContentInfo("contentInfo")
  10. .setContentTitle("ContentTitle").setContentText("ContentText")
  11. .setStyle(inboxStyle)
  12. .setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
  13. .build();
  14. manager.notify(NOTIFICATION_ID_4, notification);
  15. }

5>自定义notification

效果图:


并对中间的播放按钮做了一个简单的点击处理事件(点击播放后,请关闭幕帘则可能会看不到toast提示)

  
  
  1. private void showCustomView() {
  2.     RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.custom_notification);
  3.     Intent intent = new Intent(this, TestMusicControl.class);
  4.     PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,intent, 0);
  5.     remoteViews.setOnClickPendingIntent(R.id.paly_pause_music,pendingIntent);
  6.     NotificationCompat.Builder builder = new Builder(context);
  7.     builder.setContent(remoteViews).setSmallIcon(R.drawable.music_icon)
  8.         .setLargeIcon(icon).setOngoing(true)
  9.         .setTicker("music is playing");
  10.     manager.notify(NOTIFICATION_ID_8, builder.build());
  11. }


点击事件处理 ###########################################################################

有时候我们可能需要实现这样的功能:当新notification出现时,我们希望点击它后可直接进入应用相应的界面中去完整查看或处理此消息的功能。然后,当我们点击back按钮时返回到应用主界面而不是桌面。比如:当我们有新的短信来时,我们在任务栏中点击它后进入读信息页面,当我们读完短信后,按“返回”键回到短信的主界面,而不是手机桌面。要实现这样的功能要我们做相应的处理:

1>返回应用主界面

  
  
  1. private void backApp() {
  2. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
  3. // Adds the back stack
  4. stackBuilder.addParentStack(OtherActivity.class);
  5. // Adds the Intent to the top of the stack
  6. Intent resultIntent = new Intent(this, OtherActivity.class);
  7. stackBuilder.addNextIntent(resultIntent);
  8. // Gets a PendingIntent containing the entire back stack
  9. PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
  10. PendingIntent.FLAG_UPDATE_CURRENT);
  11. Notification notification = new NotificationCompat.Builder(context)
  12. .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
  13. .setTicker("backApp").setContentInfo("contentInfo")
  14. .setContentTitle("ContentTitle").setContentText("ContentText")
  15. .setContentIntent(resultPendingIntent).setAutoCancel(true)
  16. .setDefaults(Notification.DEFAULT_ALL).build();
  17. manager.notify(NOTIFICATION_ID_5, notification);
  18. this.finish();
  19. }

并需要我们在配置文件中对我们用来显示详细信息的OtherActivity进行相应的配置如下:

android:name="com.example.notification.OtherActivity"

android:label="@string/title_activity_other"

android:parentActivityName=".MainActivity" >

android:name="android.support.PARENT_ACTIVITY"

android:value=".MainActivity" />

2>直接返回桌面

有些时候我们可能需要实现这样的功能:当我们点击notification时弹出一个稍大点的窗口来显示整个消息,这窗口的作用就是用来显示整个消息内容的,和此应用内的其它Activity都没有关系,然后当我们点击"back"后直接返回到手机桌面。要实现这样的功能我们只需要调用builder的.setContentIntent方法,然后对所要跳转到的activity在配置文件中进行一些配置:

  
  
  1. private void backScreen() {
  2. Intent notifyIntent = new Intent(this, SpecialActivity.class);
  3. // Sets the Activity to start in a new, empty task
  4. notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK);
  5. // Creates the PendingIntent
  6. PendingIntent notify_Intent = PendingIntent.getActivity(this, 0,
  7. notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  8. Notification notification = new NotificationCompat.Builder(context)
  9. .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
  10. .setTicker("backScreen").setContentInfo("contentInfo")
  11. .setContentTitle("ContentTitle").setContentText("ContentText")
  12. .setContentIntent(notify_Intent).setAutoCancel(true)
  13. .setDefaults(Notification.DEFAULT_ALL).build();
  14. manager.notify(NOTIFICATION_ID_6, notification);
  15. this.finish();
  16. }

配置文件:

android:name="com.example.notification.SpecialActivity"

android:excludeFromRecents="true"

android:label="@string/title_activity_special"

android:launchMode="singleTask"

android:taskAffinity="" >


3>点击通知调到应用界面

Intent notificationIntent = new Intent(TaskStatusService.this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
new Notification.Builder(this).setContentIntent(pendingIntent).build();

4>点击通知发出广播
//新建意图,并设置action标记为"play",用于接收广播时过滤意图信息  
Intent intentPlay = new Intent("play");
PendingIntent pIntentPlay = PendingIntent.getBroadcast(this, 0,   intentPlay, 0);  
remoteViews.setOnClickPendingIntent(R.id.bt_notic_play, pIntentPlay);//为play控件注册事件


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值