Android Notification通知详解

Android Notification通知详解

 Notification:
(一)、简介:
        显示在手机状态栏的通知。Notification所代表的是一种具有全局效果的通知,程序一般通过NotificationManager服务来发送Notification。
        Android3.0增加了Notification.Builder类,该类可以轻松地创建Notification对象。

(二)、Notification.Builder类中提供的方法:
builder.setAutoCancel(); 设置点击通知后,状态栏自动删除通知。
builder.setSmallIcon(R.drawable.alert); 设置通知小图标
builder.setLargeIcon(R.drawable.alert2); 设置通知大图标
builder.setContentTitle("标题"); 设置通知标题
builder.setContentText("文本");  设置通知内容
builder.setDefaults(Notification.DEFAULT_SOUND
| Notification.DEFAULT_VIBRATE);  设置通知的音乐、振动、LED等。
builder.setSound();  设置通知的音乐
builder.setTick();  设置通知在状态栏的提示文本
builder.setContentIntent();  设置点击通知后将要启动的程序组件对应的PendingIntent

(三)、发送Notification的步骤:(四部曲)
1、调用getSystemService(NOTIFICATION_SERVICE)方法获取系统的NotificationManager服务,它是一个重要的系统服务。应用程序可以通过NotificationManager 向系统发送全局通知;
2、构造Notification.Builder对象;
3、设置Notification.Builder对象的各种属性;
4、通过NotificationManager 的notify()方法发送Notification。

(四)、示例代码:
核心代码如下:

NotificationManager  manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
builder = new Notification.Builder(this);
builder.setAutoCancel(true);
builder.setSmallIcon(R.drawable.alert);
builder.setContentTitle("标题");
builder.setContentText("文本");
builder.setDefaults(Notification.DEFAULT_SOUND| Notification.DEFAULT_VIBRATE);
Intent intent = new Intent(this, SecondActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 1, intent,PendingIntent.FLAG_ONE_SHOT);
builder.setContentIntent(pIntent);
manager.notify(0, builder.build());


(五)、PendingIntent:
1、PendingIntent字面意义:等待的,悬而未决的Intent;
2、得到一个 PendingIntent 对象,使用方法类的静态方法 getActivity(Context, int, Intent, int);
3、PendingIntent是一种特殊的Intent。主要的区别在于Intent是立刻执行,而 PendingIntent 的执行不是立刻,而是当条件满足后才发送企图,而且PendingIntent 可以取消;
4、PendingIntent执行的操作实质上是参数传进来的Intent的操作,使用 PendingIntent 的目的在于它所包含的Intent的操作的执行是需要满足某些条件的。
5、主要的使用的地方和例子:通知Notificatio的发送,短消息SmsManager的发送和警报器AlarmManager的执行等。

(六)、Intent和PendingIntent的区别:【掌握,以备面试之需】
  1.  Intent是立即使用的,而PendingIntent可以等到事件发生后触发,PendingIntent可以cancel
  2.  Intent在程序结束后即终止,而PendingIntent在程序结束后依然有效
  3.  PendingIntent自带Context,而Intent需要在某个Context内运行;
  4.  Intent在原task中运行,PendingIntent在新的task中运行。
七)、PendingIntent的几个常量:(getActivity(Context, int, Intent, int)方法中的第四个参数
  1. FLAG_ONE_SHOT  : 这个PendingIntent只能使用一次。
  2. FLAG_NO_CREATE : 如果被描述的PendingIntent不存在,那么简单地返回null,而不是创建它。
  3. FLAG_CANCEL_CURRENT  :  如果被描述的PendingIntent已经存在,在即将生成一个新的PendingIntent前,当前的一个要被取消。
  4. FLAG_UPDATE_CURRENT  :如果被描述的PendingIntent已经存在,那么继续保持它,但它其中的数据会因为新Intent而更新。

设置notification的点击跳转:


NotificationCompat.Builder builder = new NotificationCompat.Builder(
      MainActivity.this);
    builder.setContentText("344444444444");
    builder.setContentTitle("33333333333");
    builder.setSmallIcon(R.drawable.ic_launcher);
    Intent intent = new Intent(MainActivity.this,
      ScondActivity.class);
    PendingIntent intent2 = PendingIntent.getActivity(
      MainActivity.this, 1, intent,
      PendingIntent.FLAG_CANCEL_CURRENT);
    builder.setContentIntent(intent2);
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(1, builder.build());


/** * 在状态栏显示通知 */ private void showNotification(){ // 创建一个NotificationManager的引用 NotificationManager notificationManager = (NotificationManager) this.getSystemService(android.content.Context.NOTIFICATION_SERVICE); // 定义Notification的各种属性 Notification notification =new Notification(R.drawable.icon, "督导系统", System.currentTimeMillis()); //FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉 //FLAG_NO_CLEAR 该通知不能被状态栏的清除按钮给清除掉 //FLAG_ONGOING_EVENT 通知放置在正在运行 //FLAG_INSISTENT 是否一直进行,比如音乐一直播放,知道用户响应 notification.flags |= Notification.FLAG_ONGOING_EVENT; // 将此通知放到通知栏的"Ongoing"即"正在运行"组中 notification.flags |= Notification.FLAG_NO_CLEAR; // 表明在点击了通知栏中的"清除通知"后,此通知不清除,经常与FLAG_ONGOING_EVENT一起使用 notification.flags |= Notification.FLAG_SHOW_LIGHTS; //DEFAULT_ALL 使用所有默认值,比如声音,震动,闪屏等等 //DEFAULT_LIGHTS 使用默认闪光提示 //DEFAULT_SOUNDS 使用默认提示声音 //DEFAULT_VIBRATE 使用默认手机震动,需加上<uses-permission android:name="android.permission.VIBRATE" />权限 notification.defaults = Notification.DEFAULT_LIGHTS; //叠加效果常量 //notification.defaults=Notification.DEFAULT_LIGHTS|Notification.DEFAULT_SOUND; notification.ledARGB = Color.BLUE; notification.ledOnMS =5000; //闪光时间,毫秒 // 设置通知的事件消息 CharSequence contentTitle ="督导系统标题"; // 通知栏标题 CharSequence contentText ="督导系统内容"; // 通知栏内容 Intent notificationIntent =new Intent(MainActivity.this, MainActivity.class); // 点击该通知后要跳转的Activity PendingIntent contentItent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, contentTitle, contentText, contentItent); // 把Notification传递给NotificationManager notificationManager.notify(0, notification); } ? //删除通知 private void clearNotification(){ // 启动后删除之前我们定义的通知 NotificationManager notificationManager = (NotificationManager) this
                .getSystemService(NOTIFICATION_SERVICE);   
        notificationManager.cancel(0); 
}}

1. [代码][Java]代码     

Android Notification通知详解

根据activity的生命周期,在activity不显示时,会执行onStop函数(比如按下home键),所以你在onStop函数(按退出键除外)里面把notification放在通知栏里,再此显示时,把notification从通知栏里去掉。或者,只要程序在运行就一直显示通知栏图标。

        

下面对Notification类中的一些常量,字段,方法简单介绍一下:
常量:
DEFAULT_ALL    使用所有默认值,比如声音,震动,闪屏等等
DEFAULT_LIGHTS 使用默认闪光提示
DEFAULT_SOUNDS 使用默认提示声音
DEFAULT_VIBRATE 使用默认手机震动 
【说明】:加入手机震动,一定要在manifest.xml中加入权限:
<uses-permission android:name="android.permission.VIBRATE" />
以上的效果常量可以叠加,即通过
notification.defaults =DEFAULT_SOUND|DEFAULT_VIBRATE;  
notification.defaults |= DEFAULT_SOUND (最好在真机上测试,震动效果模拟器上没有)

            

//设置flag位
FLAG_AUTO_CANCEL  该通知能被状态栏的清除按钮给清除掉
FLAG_NO_CLEAR     该通知能被状态栏的清除按钮给清除掉
FLAG_ONGOING_EVENT 通知放置在正在运行
FLAG_INSISTENT 是否一直进行,比如音乐一直播放,知道用户响应

          

常用字段:
contentIntent  设置PendingIntent对象,点击时发送该Intent
defaults 添加默认效果
flags 设置flag位,例如FLAG_NO_CLEAR等
icon 设置图标
sound 设置声音
tickerText 显示在状态栏中的文字
when 发送此通知的时间戳

                

NotificationManager常用方法介绍:
public void cancelAll() 移除所有通知(只是针对当前Context下的Notification)
public  void cancel(int id) 移除标记为id的通知 (只是针对当前Context下的所有Notification)
public  void notify(String tag ,int id, Notification notification) 将通知加入状态栏,标签为tag,标记为id
public  void notify(int id, Notification notification) 将通知加入状态栏,标记为id
posted @ 2015-12-29 21:47  恋恋西风  阅读(326)  评论(0编辑  收藏  举报