转 android notification 的总结分析

简介:

分类

 Android的notification有以下几种:

1>普通notification

104540_W8Bq_1434710.png

图1

  1. 标题,通过NotificationCompat.Builder.setContentTitle(String title)来设置

  2. 大图标,通过NotificationCompat.Builder.setLargeIcon(Bitmap icon)来设置

  3. 内容,通过NotificationCompat.Builder.setContentText("ContentText")来设置

  4. 内容附加信息通过NotificationCompat.Builder.setContentInfo("ContentInfo")来设置

  5. 小图标,通过NotificationCompat.Builder.setSmallIcon(int icon)来设置

  6. 时间,通过NotificationCompat.Builder.setWhen(when)来设置

注:

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

  • 小图标, set by setSmallIcon()

  • 标题, set by setContentTitle()

  • 内容, set by setContentText()

2>大布局Notification

104632_737g_1434710.png

图2

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

105537_AgKH_1434710.png

图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:

105749_cf6J_1434710.png

图4

创建自定义的notification

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

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

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

    4>获取系统负责通知的NotificationManager;如:manager

    5>调用manager的notify方法。


示例代码

示例程序截图:

112054_tp0u_1434710.png

图5

0>初始化部分代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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 ;
 
     @Override
     protected  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 );
         }
     }
 
     @Override
     public  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

?
1
2
3
4
5
6
7
8
9
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
效果图:

113452_0mfP_1434710.png

图6

并对中间的播放按钮做了一个简单的点击处理事件:

?
1
2
3
4
5
6
7
8
9
10
11
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());
}

布局文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<? 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"  >
 
      < ImageView
          android:id = "@+id/singer_pic"
          android:layout_width = "64dp"
          android:layout_height = "64dp"
          android:src = "@drawable/singer"  />
 
      < LinearLayout
          android:layout_width = "fill_parent"
          android:layout_height = "fill_parent"
          android:gravity = "center_vertical"
          android:orientation = "horizontal"  >
 
          < ImageView
              android:id = "@+id/last_music"
              android:layout_width = "0dp"
              android:layout_height = "48dp"
              android:layout_weight = "1"
              android:src = "@drawable/player_previous"  />
 
          < ImageView
              android:id = "@+id/paly_pause_music"
              android:layout_width = "0dp"
              android:layout_height = "48dp"
              android:layout_weight = "1"
              android:src = "@drawable/player_pause"  />
 
          < ImageView
              android:id = "@+id/next_music"
              android:layout_width = "0dp"
              android:layout_height = "48dp"
              android:layout_weight = "1"
              android:src = "@drawable/player_next"  />
      </ LinearLayout >
  </ LinearLayout >

带进度条的notification:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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() {
         @Override
         public  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 seconds
                     Thread.sleep( 2  1000 );
                 catch  (InterruptedException e) {
                 }
             }
             builder.setContentTitle( "Download complete" )
                     .setProgress( 0 0 false ).setOngoing( false );
             manager.notify(NOTIFICATION_ID_PROGRESS, builder.build());
         }
     }).start();
}
原文地址: http://www.jb51.net/article/36567.htm

目录
相关文章
|
28天前
|
搜索推荐 Android开发 iOS开发
安卓与iOS系统的用户界面设计对比分析
本文通过对安卓和iOS两大操作系统的用户界面设计进行对比分析,探讨它们在设计理念、交互方式、视觉风格等方面的差异及各自特点,旨在帮助读者更好地理解和评估不同系统的用户体验。
19 1
|
2月前
|
Android开发 数据安全/隐私保护 iOS开发
安卓与iOS系统的发展趋势与比较分析
【2月更文挑战第6天】 在移动互联网时代,安卓和iOS系统作为两大主流移动操作系统,各自呈现出不同的发展趋势。本文将从技术角度出发,对安卓和iOS系统的发展方向、特点及未来趋势进行比较分析,以期为读者提供更深入的了解和思考。
35 4
|
3月前
|
监控 Android开发 C语言
深度解读Android崩溃日志案例分析2:tombstone日志
深度解读Android崩溃日志案例分析2:tombstone日志
83 0
|
3月前
|
数据安全/隐私保护 Android开发
2023安卓逆向 -- 某合伙apk登录加密分析
2023安卓逆向 -- 某合伙apk登录加密分析
26 0
|
4月前
|
XML Java Android开发
Android Studio App开发之通知推送Notification的讲解及实战(给用户推送信息实战)
Android Studio App开发之通知推送Notification的讲解及实战(给用户推送信息实战)
149 0
|
2月前
|
网络协议 算法 Android开发
安卓逆向 -- 实战某峰窝APP(动态分析)
安卓逆向 -- 实战某峰窝APP(动态分析)
29 4
|
2月前
|
安全 搜索推荐 Android开发
Android 与 iOS 的比较分析
【2月更文挑战第5天】 Android 和 iOS 是目前市场上两种最流行的移动操作系统,它们都拥有自己的特点和优势。本文将会分别从操作系统设计、应用生态、安全性等方面对这两种操作系统进行比较和分析,希望能够帮助读者更好地选择适合自己的移动设备。
|
3月前
|
安全 算法 JavaScript
安卓逆向 -- 关键代码定位与分析技术
安卓逆向 -- 关键代码定位与分析技术
41 0
|
3月前
|
算法 安全 Android开发
安卓逆向 -- Frida Hook某车_sign算法分析
安卓逆向 -- Frida Hook某车_sign算法分析
76 0
|
3月前
|
安全 Android开发 数据安全/隐私保护
安卓逆向 -- SO文件逆向分析
安卓逆向 -- SO文件逆向分析
20 0