为什么80%的码农都做不了架构师?>>>   

分类

Android的notification有以下几种:

1>普通notification

图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

图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 的总结分析相关推荐

  1. android notification 的总结分析,Android Notification的多种用法总结

    android notification的多种用法总结 我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今 ...

  2. android notification 的总结分析,Android中Notification用法实例总结

    本文实例总结了 Android中Notification用法.分享给大家供大家参考,具体如下: 我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图 ...

  3. Android ADB 源码分析(三)

    前言 之前分析的两篇文章 Android Adb 源码分析(一) 嵌入式Linux:Android root破解原理(二) 写完之后,都没有写到相关的实现代码,这篇文章写下ADB的通信流程的一些细节 ...

  4. Android以太网框架情景分析之NetworkFactory与NetworkAgent深入分析

    Android以太网框架情景分析之NetworkFactory与NetworkAgent深入分析 Android网络框架分析系列文章目录: Android P适配以太网功能开发指南 Android以太 ...

  5. Android后台保活套路分析

    Android后台保活套路分析 原文作者:D_clock爱吃葱花 链接:https://www.jianshu.com/p/63aafe3c12af 來源:简书 基于个人理解进行了部分删减补充 保活手 ...

  6. [原创]Android Monkey 在线日志分析工具开发

    [原创]Android Monkey 在线日志分析工具开发 在移动App测试过程中,Monkey测试是我们发现潜在问题的一种非常有效手段,但是Android原生的Monkey有其天然的不足,数据不能有 ...

  7. android应用内存分析,Android应用程序内存分析-Memory Analysis for Android Applications

    Android应用程序内存分析 原文链接:http://android-developers.blogspot.com/2011/03/memory-analysis-for-android.html ...

  8. Android Hal层简要分析

    Android Hal层简要分析 Android Hal层(即 Hardware Abstraction Layer)是Google开发的Android系统里上层应用对底层硬件操作屏蔽的一个软件层次, ...

  9. Android逆向与病毒分析

    本文由同程旅游安全团队对内移动安全培训的PPT整理而来,面向对象为对移动安全感兴趣的研发同事,所以讲的有些宽泛.介绍了入门Android逆向需要掌握的一些知识点, 通过简单的几个案例讲解Android ...

最新文章

  1. 耿直:统计学中的因果推断问题(Causal Inference)
  2. 面试题-两个数值交换
  3. 建立行政效果公布体制
  4. 蓝图Blueprint
  5. SqlServer 算法 :Nested Loops Join(嵌套连接)
  6. 三维重建10:点云配准和点云匹配
  7. 【转】Java MySQL数据类型对照
  8. javascript 点点滴滴01章 javascript的认知
  9. Docker1.12.6+CentOS7.3 的安装
  10. vjc机器人灰度怎么编程_求用vc++编程实现显示灰度直方图的详细步骤,越详细越好...
  11. 本特利振动前置器330180-51-00
  12. 百度杯全国网络攻防大赛——初来乍到
  13. 小信号放大运算放大器使用要依照三步骤,4个细节更重要
  14. 实验四: IPv6路由选择协议配置
  15. Vue2父传子、子传父和兄弟间互传
  16. echars基本使用
  17. log4j漏洞分析及总结
  18. 微信小程序----性别选择,并实现数据渲染
  19. java学习日记-接口
  20. 【java】对URL中的中文和符号进行UrlEncode转码

热门文章

  1. SQLServer之PRIMARY KEY约束
  2. SK-Learn使用NMF(非负矩阵分解)和LDA(隐含狄利克雷分布)进行话题抽取
  3. 简单说下COALESCE这个日常使用的函数
  4. Windows计算机功能Java源码
  5. Windows消息机制详解-5
  6. MFC中动态创建控件以及添加事件响应
  7. 总结以下三种方法,实现c#每隔一段时间执行代码:
  8. IOS基础之iPad的屏幕旋转方向判断
  9. IOS开发基础之图片轮播器-12
  10. python 二分类的实例_keras分类之二分类实例(Cat and dog)