Android开发之通知栏Notification详解

Notification的用法  --- 状态栏通知


发送一个状态栏通知必须的两个类:
        
1. NotificationManager   --- 状态栏通知的管理类,负责发通知,清除通知等
   NotificationManager : 是一个系统Service,必须通过 context.getSystemService(NOTIFICATION_SERVICE)方法获取
   NotificationManager notificationManager = (NotificationManager) context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);


2.  Notification         --- 具体的状态栏通知对象,可以设置icon,文字,提示音,震动等参数
           下面是设置一个通知需要的基本参数
  Anicon(通知的图标)
  Atitleanexpandedmessage(通知的标题和内容)
  APendingIntent(点击通知执行页面跳转)

   1.创建Notification
          通过NotificationManager的notify(int Id , Notification)方法来启动Notification
          参数一:Notification的唯一标识
 参数二:Notification对象

   2.更新Notification
          调用Notification的setLatestEventInfo()方法来更新内容,然后调用NotificationManager的notify()方法即可

   3.删除Notification
          通过NotificationManager的cancle(int id) 方法,清除通知    参数: 要清除的Notification的唯一标识

   

   4.Notification设置  -- 震动,铃声等

          1.基本设置:

                            

//新建状态栏通知 
baseNF=new Notification(); 
//设置通知在状态栏显示的图标 
baseNF.icon=R.drawable.icon; 
//通知时在状态栏显示的内容 
baseNF.tickerText="YouclickedBaseNF!"; 
//通知的默认参数DEFAULT_SOUND,DEFAULT_VIBRATE,DEFAULT_LIGHTS. 
//如果要全部采用默认值,用DEFAULT_ALL. 
//此处采用默认声音 
baseNF.defaults=Notification.DEFAULT_SOUND; 
//第二个参数:下拉状态栏时显示的消息标题expandedmessagetitle 
//第三个参数:下拉状态栏时显示的消息内容expandedmessagetext 
//第四个参数:点击该通知时执行页面跳转 
baseNF.setLatestEventInfo(Lesson_10.this,"Title01","Content01",pd); 
//发出状态栏通知 
//ThefirstparameteristheuniqueIDfortheNotification 
//andthesecondistheNotificationobject. 
nm.notify(Notification_ID_BASE,baseNF); 
 

      2.添加声音

       baseNF.default=Notification.DEFAULT_SOUND;    -- 使用系统默认提示音

       notification.sound=Uri.parse("file:///sdcard/notification/ringer.mp3");   --- 自定义声音

     使用用系统自带的铃声,可以这样: 

      notification.sound=Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI,"6"); 

      3.添加震动

        notification.defaults|=Notification.DEFAULT_VIBRATE;       使用默认震动方式

      4.添加闪光

       notification.defaults|=Notification.DEFAULT_LIGHTS; 

    5.其他有用的设置

flags: 
Notification.FLAG_INSISTENT;//让声音、振动无限循环,直到用户响应 
Notification.FLAG_AUTO_CANCEL;//通知被点击后,自动消失 
Notification.FLAG_NO_CLEAR;//点击'Clear'时,不清楚该通知(QQ的通知无法清除,就是用的这个 
//自定义下拉视图,比如下载软件时,显示的进度条。 
Notificationnotification=newNotification(); 
notification.icon=R.drawable.icon; 
notification.tickerText="Custom!"; 
RemoteViewscontentView=newRemoteViews(getPackageName(),R.layout.custom); 
contentView.setImageViewResource(R.id.image,R.drawable.icon); 
contentView.setTextViewText(R.id.text,"Hello,thismessageisinacustomexpandedview"); 
notification.contentView=contentView; 
//使用自定义下拉视图时,不需要再调用setLatestEventInfo()方法 
//但是必须定义contentIntent 
notification.contentIntent=pd; 
nm.notify(3,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


package com.ljq.activity;
 
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
 
public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        clearNotification();
    }
     
    @Override
    protected void onStop() {
        showNotification();
        super.onStop();
    }
     
    @Override
    protected void onStart() {
        clearNotification();
        super.onStart();
    }
     
    /**
     * 在状态栏显示通知
     */
    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);  
 
    }
}

应用实例二:

我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的。

[java]  view plain copy
  1. package cn.com.chenzheng_java;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.Notification;  <
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值