android notification 的总结分析

   2015-08-24 0
核心提示:notification是一种出现在任务栏的提示,特别是在4.0以后notification改进了不少,本文内容都是基于4.0及4.1以后总结来的

分类

 notification有以下几种:

  1>普通notification

  android notification 的总结分析

    1.内容标题

    2.大图标

    3.内容

    4.内容附加信息

    5.小图标

    6.时间

  2>大布局Notification

    android notification 的总结分析图1

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

  android notification 的总结分析图2

    大布局notification有三种类型:如图1为NotificationCompat.InboxStyle 类型。图2左部为NotificationCompat.BigTextStyle。图2右部 为:NotificationCompat.BigPictureStyle

   3>自定义布局notification

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

    android notification 的总结分析图3

如何创建notification


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

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

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

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

     5>调用manager的notify方法。

  注:

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

   小图标, set by setSmallIcon()

     内容标题, set by setContentTitle()

     内容, set by setContentText()

示例代码



示例程序截图:

 android notification 的总结分析

0>初始化部分代码

复制代码 代码如下:

View Code
 public class MainActivity extends Activity implements OnClickListener {

     private static final int NOTIFICATION_ID_1 = 0;
     private static final int NOTIFICATION_ID_2 = 1;
     private static final int NOTIFICATION_ID_3 = 2;
     private static final int NOTIFICATION_ID_4 = 3;
     private static final int NOTIFICATION_ID_5 = 4;
     private static final int NOTIFICATION_ID_6 = 5;
     private static final int NOTIFICATION_ID_7 = 6;
     private static final int NOTIFICATION_ID_8 = 7;

     private static int messageNum = 0;
     private Context context = this;
     private NotificationManager manager;
     private Bitmap icon;
     private static final int[] btns = new int[] { R.id.btn1, R.id.btn2,
             R.id.btn3, R.id.btn4, R.id.btn5, R.id.btn6, R.id.btn7, R.id.btn8,
             R.id.btn9 };

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         init();
     }

     private void init() {
         // 获取通知服务
         manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
         // 注册监听器
         for (int btn : btns) {
             findViewById(btn).setOnClickListener(this);
         }

         icon = BitmapFactory.decodeResource(getResources(),
                 R.drawable.ic_launcher);
     }

     @Override
     public void onClick(View v) {
         switch (v.getId()) {
         case R.id.btn1:
             showNormal();
             break;
         case R.id.btn2:
             showBigView_Text();
             break;
         case R.id.btn3:
             showBigView_Pic();
             break;
         case R.id.btn4:
             showBigView_Inbox();
             break;
         case R.id.btn5:
             showCustomView();
             break;
         case R.id.btn6:
             backApp();
             break;
         case R.id.btn7:
             backScreen();
             break;
         case R.id.btn8:
             showProgressBar();
             break;
         case R.id.btn9:
             dismiss();
             break;
         default:
             Toast.makeText(context, "error", Toast.LENGTH_SHORT).show();
             break;
         }
     }

     private void dismiss() {
         manager.cancelAll();
     }

1>普通notification
复制代码 代码如下:

View Code
     private void showNormal() {

         Notification notification = new NotificationCompat.Builder(context)
                 .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
                 .setTicker("showNormal").setContentInfo("contentInfo")
                 .setContentTitle("ContentTitle").setContentText("ContentText")
                 .setNumber(++messageNum)
                 .setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
                 .build();
         manager.notify(NOTIFICATION_ID_1, notification);
     }

2>大布局Text类型notification
复制代码 代码如下:

View Code
 private void showBigView_Text() {
         NotificationCompat.BigTextStyle textStyle = new BigTextStyle();
         textStyle
                 .setBigContentTitle("BigContentTitle")
                 .setSummaryText("SummaryText")
                 .bigText(
                         "I am Big Texttttttttttttttttttttttttttttttttttttttttttt!!!!!!!!!!!!!!!!!!!......");
         Notification notification = new NotificationCompat.Builder(context)
                 .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
                 .setTicker("showBigView_Text").setContentInfo("contentInfo")
                 .setContentTitle("ContentTitle").setContentText("ContentText")
                 .setStyle(textStyle)
                 .setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
                 .build();
         manager.notify(NOTIFICATION_ID_2, notification);
     }

3> 大布局Picture类型notificatio
复制代码 代码如下:

View Code
 private void showBigView_Pic() {
         NotificationCompat.BigPictureStyle pictureStyle = new BigPictureStyle();
         pictureStyle.setBigContentTitle("BigContentTitle")
                 .setSummaryText("SummaryText").bigPicture(icon);
         Notification notification = new NotificationCompat.Builder(context)
                 .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
                 .setTicker("showBigView_Pic").setContentInfo("contentInfo")
                 .setContentTitle("ContentTitle").setContentText("ContentText")
                 .setStyle(pictureStyle)
                 .setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
                 .build();
         manager.notify(NOTIFICATION_ID_3, notification);
     }

4>大布局Inbox类型notification
复制代码 代码如下:

View Code
 private void showBigView_Inbox() {
         NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
         inboxStyle.setBigContentTitle("BigContentTitle").setSummaryText(
                 "SummaryText");
         for (int i = 0; i < 5; i++)
             inboxStyle.addLine("news:" + i);
         Notification notification = new NotificationCompat.Builder(context)
                 .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_4, notification);
     }

5>自定义notification
效果图:

android notification 的总结分析

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

复制代码 代码如下:

View Code
 private void showCustomView() {
         RemoteViews remoteViews = new RemoteViews(getPackageName(),
                 R.layout.custom_notification);
         Intent intent = new Intent(this, TestMusicControl.class);
         PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
                 intent, 0);
         remoteViews.setOnClickPendingIntent(R.id.paly_pause_music,
                 pendingIntent);
         NotificationCompat.Builder builder = new Builder(context);
         builder.setContent(remoteViews).setSmallIcon(R.drawable.music_icon)
                 .setLargeIcon(icon).setOngoing(true)
                 .setTicker("music is playing");
         manager.notify(NOTIFICATION_ID_8, builder.build());
     }

布局文件:
复制代码 代码如下:

View Code
 <?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/songer_pic"
         android:layout_width="64dp"
         android:layout_height="64dp"
         android:src="@drawable/yan" />

     <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/music_previous" />

         <ImageView
             android:id="@+id/paly_pause_music"
             android:layout_width="0dp"
             android:layout_height="48dp"
             android:layout_weight="1"
             android:src="@drawable/music_play" />

         <ImageView
             android:id="@+id/next_music"
             android:layout_width="0dp"
             android:layout_height="48dp"
             android:layout_weight="1"
             android:src="@drawable/music_next" />
     </LinearLayout>

 </LinearLayout>

带进度条的notification
复制代码 代码如下:

View Code
     private void showProgressBar() {

         final NotificationCompat.Builder builder = new NotificationCompat.Builder(
                 context);
         builder.setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
                 .setTicker("showProgressBar").setContentInfo("contentInfo")
                 .setOngoing(true).setContentTitle("ContentTitle")
                 .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, false);
                     manager.notify(NOTIFICATION_ID_7, builder.build());
                     try {
                         // Sleep for 5 seconds
                         Thread.sleep(2 * 1000);
                     } catch (InterruptedException e) {
                         System.out.println("sleep failure");
                     }
                 }
                 builder.setContentTitle("Download complete")
                         .setProgress(0, 0, false).setOngoing(false);
                 manager.notify(NOTIFICATION_ID_7, builder.build());

             }
         }).start();
     }

点击事件处理

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

1>返回应用主界面

复制代码 代码如下:

View Code
     private void backApp() {

         TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
         // Adds the back stack
         stackBuilder.addParentStack(OtherActivity.class);
         // Adds the Intent to the top of the stack
         Intent resultIntent = new Intent(this, OtherActivity.class);
         stackBuilder.addNextIntent(resultIntent);
         // Gets a PendingIntent containing the entire back stack
         PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
                 PendingIntent.FLAG_UPDATE_CURRENT);

         Notification notification = new NotificationCompat.Builder(context)
                 .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
                 .setTicker("backApp").setContentInfo("contentInfo")
                 .setContentTitle("ContentTitle").setContentText("ContentText")
                 .setContentIntent(resultPendingIntent).setAutoCancel(true)
                 .setDefaults(Notification.DEFAULT_ALL).build();
         manager.notify(NOTIFICATION_ID_5, notification);
         this.finish();
     }

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

<activity
             android:name="com.example.notification.OtherActivity"
             android:label="@string/title_activity_other"
             android:parentActivityName=".MainActivity" >
             <meta-data
                 android:name="android.support.PARENT_ACTIVITY"
                 android:value=".MainActivity" />
         </activity>

2>直接返回桌面

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

复制代码 代码如下:

View Code
 private void backScreen() {
         Intent notifyIntent = new Intent(this, SpecialActivity.class);
         // Sets the Activity to start in a new, empty task
         notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                 | Intent.FLAG_ACTIVITY_CLEAR_TASK);
         // Creates the PendingIntent
         PendingIntent notify_Intent = PendingIntent.getActivity(this, 0,
                 notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

         Notification notification = new NotificationCompat.Builder(context)
                 .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)
                 .setTicker("backScreen").setContentInfo("contentInfo")
                 .setContentTitle("ContentTitle").setContentText("ContentText")
                 .setContentIntent(notify_Intent).setAutoCancel(true)
                 .setDefaults(Notification.DEFAULT_ALL).build();
         manager.notify(NOTIFICATION_ID_6, notification);
         this.finish();
     }

配置文件:
复制代码 代码如下:

 <activity
             android:name="com.example.notification.SpecialActivity"
             android:excludeFromRecents="true"
             android:label="@string/title_activity_special"
             android:launchMode="singleTask"
             android:taskAffinity="" >

源码下载

 
标签: android notification
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • 说一说Android Studio和IDEA中一个很有用的内存调试插件
    说一说Android Studio和IDEA中一个很有用的内存
    JetBrains JVM Debugger Memory View plugin 在我最近的研发活动期间寻找新的工具,以提高我的开发经验,使Android Studio的生活更轻松,我发现一个有用的插件,我从来没有听说过。 这就是为什么,我决定写这个强大的工具,它如何帮助我与内存调试我的应用程
  • 安卓中通知功能的具体实现
    安卓中通知功能的具体实现
    通知[Notification]是Android中比较有特色的功能,当某个应用程序希望给用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知实现。使用通知的步骤1、需要一个NotificationManager来获得NotificationManager manager = (NotificationManager
    02-05 安卓开发
  • Android view系统分析-setContentView
    Android view系统分析-setContentView
    第一天上班,列了一下今年要学习的东西。主要就是深入学习Android相关的系统源代码,夯实基础。对于学习Android系统源代码,也没什么大概,就从我们平常使用最基础的东西学起,也就是从view这个切入点开始学习Android的源码,在没分析源码之前,我们有的时候
    02-05 安卓开发
  • 如何进行网络视频截图/获取视频的缩略图
    如何进行网络视频截图/获取视频的缩略图
    小编导读:获取视频的缩略图,截图正在播放的视频某一帧,是在音视频开发中,常遇到的问题。本文是主要用于点播中截图视频,同时还可以获取点播视频的缩略图进行显示,留下一个问题,如下图所示, 如果要获取直播中节目视频缩略图,该怎么做呢?(ps:直播是直
  • Android NDK 层发起 HTTP 请求的问题及解决
    Android NDK 层发起 HTTP 请求的问题及解决
    前言新的一年,大家新年快乐~~鸡年大吉!本次给大家带来何老师的最新文章~虽然何老师还在过节,但依然放心不下广大开发者,在此佳节还未结束之际,给大家带来最新的技术分享~ 事件的起因不说了,总之是需要实现一个 NDK 层的网络请求。为了多端适用,还是选择
  • SDK热更之如何在SDK代码中自动插桩及如何生成补
    写在前面本文是SDKHotfix相关的SDK热更系列文章中的一篇,以下为项目及系列文章相关链接:SDKHotfix整体介绍:http://blog.bihe0832.com/sdk_hotfix_project.htmlSDKHotfix对应github地址:https://github.com/bihe0832/SDKHoxFix这篇文章主要介绍一下SDK热更
  • 安装量破千万的第一个产品,我总结了3句话
    安装量破千万的第一个产品,我总结了3句话
    在今天的文章中,作者回顾了自己的第一个产品,他说“我做的第一款产品,是我的一块里程碑。”一起来看看~背景老牌大型互联网公司,部门内部创业的一个项目。我作为产品经理,也是第一次做产品经理,主导产品项目。实际上,项目初期包括我和安卓开发2个人。开
  • 移动周刊第 176 期:Android 知识梳理
    移动周刊第 176 期:Android 知识梳理
    写在前面 本期移动周刊第 176 期如约而至,聚焦 Android、iOS、VR/AR/MR、直播等前沿移动开发技术,收录一周最热点,解读开发技巧,每周三移动周刊抢先看,我们希望从中能够让你有一些收获,如果你有好的文章以及优化建议,请发送邮件至mobilehub@csdn.net,
  • Android插件化(六): OpenAtlasの改写aapt以防止资源ID冲突
    Android插件化(六): OpenAtlasの改写aapt以防
    引言Android应用程序的编译中,负责资源打包的是aapt,如果不对打包后的资源ID进行控制,就会导致插件中的资源ID冲突。所以,我们需要改写aapt的源码,以达到通过某种方式传递资源ID的Package ID,通过aapt打包时获取到这个Package ID并且应用才插件资源的命名
    02-05 安卓开发
  • Android架构(一)MVP架构在Android中的实践
    Android架构(一)MVP架构在Android中的实践
    为什么要重视程序的架构设计 对程序进行架构设计的原因,归根结底是为了 提高生产力 。通过设计是程序模块化,做到模块内部的 高聚合 和模块之间的 低耦合 (如依赖注入就是低耦合的集中体现)。 这样做的好处是使得程序开发过程中,开发人员主需要专注于一点,
    02-05 安卓开发
点击排行