当用户有没有接到的电话的时候,Android顶部状态栏里就会出现一个小图标。提示用户有没有处理的快讯,当拖动状态栏时,可以查看这些快讯。Android给我们提供了NotificationManager来管理这个状态栏。可以很轻松的完成。

(您想上youtube、facebook吗?了解最新国际国内形势吗?请发邮件到freeget.one@gmail.com获得软件)

如果要添加一个Notification,可以按照以下几个步骤

1:获取NotificationManager:

NotificationManager m_NotificationManager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);

2:定义一个Notification:

Notification  m_Notification=new Notification();

3:设置Notification的各种属性:

//设置通知在状态栏显示的图标
m_Notification.icon=R.drawable.icon;
               
 //当我们点击通知时显示的内容
m_Notification.tickerText="Button1 通知内容.....";
                               
通知时发出的默认声音
m_Notification.defaults=Notification.DEFAULT_SOUND;

//设置通知显示的参数

Intent   m_Intent=new Intent(NotificationDemo.this,DesActivity.class);      
PendingIntent m_PendingIntent=PendingIntent.getActivity(NotificationDemo.this, 0, m_Intent, 0);

m_Notification.setLatestEventInfo(NotificationDemo.this, "Button1", "Button1通知",m_PendingIntent );

//这个可以理解为开始执行这个通知
m_NotificationManager.notify(0,m_Notification);

4:既然可以增加同样我们也可以删除。当然是只是删除你自己增加的。

m_NotificationManager.cancel(0);

这里的0是一个ID号码,和notify第一个参数0一样。

这也就完成了,添加删除工作。

这里我们还是一个Demo来掩饰我们的操作。

1:新建一个工程NotificationDemo。

2:添加一个Activity:DesActivity,注意需要在Manifest.xml中声明

3:NotificationDemo中的Laytout文件很简单就是定义一个Button.其代码文件如下:

view plaincopy to clipboardprint?
package com.rocky.studio.ch4221;  
import android.app.Activity;  
import android.app.Notification;  
import android.app.NotificationManager;  
import android.app.PendingIntent;  
import android.content.Intent;  
import android.os.Bundle;  
import android.view.View;  
import android.widget.Button;  
import android.widget.TextView;  
public class NotificationDemo extends Activity {  
      
    Button m_Button1;  
    TextView m_txtView;  
      
    NotificationManager m_NotificationManager;  
    Notification m_Notification;  
      
    Intent m_Intent;  
    PendingIntent m_PendingIntent;  
          
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
          
        m_NotificationManager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);  
          
                  
        m_Button1=(Button)this.findViewById(R.id.Button01);  
         
          
        //点击通知时转移内容  
        m_Intent=new Intent(NotificationDemo.this,DesActivity.class);  
          
        m_PendingIntent=PendingIntent.getActivity(NotificationDemo.this, 0, m_Intent, 0);  
          
        m_Notification=new Notification();  
          
        m_Button1.setOnClickListener(new Button.OnClickListener(){  
            public void onClick(View v) {  
                // TODO Auto-generated method stub  
                  
                //设置通知在状态栏显示的图标  
                m_Notification.icon=R.drawable.icon;  
                  
                //当我们点击通知时显示的内容  
                m_Notification.tickerText="Button1 通知内容.....";  
                                  
                //通知时发出的默认声音  
                m_Notification.defaults=Notification.DEFAULT_SOUND;  
                  
                //设置通知显示的参数  
                m_Notification.setLatestEventInfo(NotificationDemo.this, "Button1", "Button1通知",m_PendingIntent );  
                  
                //这个可以理解为开始执行这个通知  
                m_NotificationManager.notify(0,m_Notification);  
                  
            }});  
          
    }  
      
      

package com.rocky.studio.ch4221;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class NotificationDemo extends Activity {
 
 Button m_Button1;
 TextView m_txtView;
 
 NotificationManager m_NotificationManager;
 Notification m_Notification;
 
 Intent m_Intent;
 PendingIntent m_PendingIntent;
  
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        m_NotificationManager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
       
               
        m_Button1=(Button)this.findViewById(R.id.Button01);
      
       
        //点击通知时转移内容
        m_Intent=new Intent(NotificationDemo.this,DesActivity.class);
       
        m_PendingIntent=PendingIntent.getActivity(NotificationDemo.this, 0, m_Intent, 0);
       
        m_Notification=new Notification();
       
        m_Button1.setOnClickListener(new Button.OnClickListener(){
   public void onClick(View v) {
    // TODO Auto-generated method stub
    
    //设置通知在状态栏显示的图标
    m_Notification.icon=R.drawable.icon;
    
    //当我们点击通知时显示的内容
    m_Notification.tickerText="Button1 通知内容.....";
        
    //通知时发出的默认声音
    m_Notification.defaults=Notification.DEFAULT_SOUND;
    
    //设置通知显示的参数
    m_Notification.setLatestEventInfo(NotificationDemo.this, "Button1", "Button1通知",m_PendingIntent );
    
    //这个可以理解为开始执行这个通知
    m_NotificationManager.notify(0,m_Notification);
    
   }});
       
    }
   
   
}

4:修改DesActivity 的源文件,代码如下:它做的事情就是取消之前添加的Notification

view plaincopy to clipboardprint?
01.package com.rocky.studio.ch4221;  
02.import android.app.Activity;  
03.import android.app.NotificationManager;  
04.import android.os.Bundle;  
05.public class DesActivity extends Activity {  
06.      
07.    NotificationManager m_NotificationManager;  
08.      
09.    @Override 
10.    protected void onCreate(Bundle savedInstanceState) {  
11.        // TODO Auto-generated method stub  
12.         super.onCreate(savedInstanceState);          
13.         this.setContentView(R.layout.main2);  
14.           
15.         //启动后删除之前我们定义的  
16.         m_NotificationManager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);  
17.         m_NotificationManager.cancel(0);          
18.    }  
19.      
20.} 
package com.rocky.studio.ch4221;
import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;
public class DesActivity extends Activity {
 
 NotificationManager m_NotificationManager;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
   super.onCreate(savedInstanceState);  
   this.setContentView(R.layout.main2);
  
   //启动后删除之前我们定义的
   m_NotificationManager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
   m_NotificationManager.cancel(0);  
 }
 
}

代码也很简单。可以查看Notification , NotificationMananger 这两个类来学习前后左右。

下面是一篇文章,对Notification ,NotificationManager这两个类有详细的说明介绍,特借鉴一下。

NoticificationManager很容易可以放在状态栏,也很容易实现从statusbar进入程序 中,
NoticificationManager中通过intent执行此程序的activity就可以了

NoticificationManager状态栏操作

NotificationManager(通知管理器):
NotificationManager负责通知用户事件的发生.
NotificationManager有三个公共方法:
1. cancel(int id) 取消以前显示的一个通知.假如是一个短暂的通知,试图将隐藏,假如是一个持久的通知,将从状态条中移走.
2. cancelAll() 取消以前显示的所有通知.
3. notify(int id,  Notification notification) 把通知持久的发送到状态条上.

//初始化NotificationManager:
NotificationManager nm =
      (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

Notification代表着一个通知.
Notification的属性:
audioStreamType 当声音响起时,所用的音频流的类型
contentIntent 当通知条目被点击,就执行这个被设置的Intent.
contentView 当通知被显示在状态条上的时候,同时这个被设置的视图被显示.
defaults 指定哪个值要被设置成默认的.
deleteIntent 当用户点击"Clear All Notifications"按钮区删除所有的通知的时候,这个被设置的Intent被执行.
icon 状态条所用的图片.
iconLevel 假如状态条的图片有几个级别,就设置这里.
ledARGB LED灯的颜色.
ledOffMS LED关闭时的闪光时间(以毫秒计算)
ledOnMS LED开始时的闪光时间(以毫秒计算)
number 这个通知代表事件的号码
sound 通知的声音
tickerText 通知被显示在状态条时,所显示的信息
vibrate 振动模式.
when 通知的时间戳.

将Notification发送到状态条上:
Notification notification = new Notification();
Notification的设置过程……..
nm.notify(0, notification);   //发送到状态条上

创建和触发Notification

创建和配置新的Notification需要经历三步。
  
  首先,你要创建一个新的Notification对象,传入要在状态条上显示的图 标、文字和Notification的 当前时间,如下面的代码片段所示:
  
  // Choose a drawable to display as the status bar icon
  int icon = R.drawable.icon;
  
  // Text to display in the status bar when the notification is launched
  String tickerText = “Notification”;
  
  // The extended status bar orders notification in time order
  long when = System.currentTimeMillis();
  Notification notification = new Notification(icon, tickerText, when);
  
  当Notification触发时,文本将沿着状态条进行滚动 显示。

其次,使用setLatestEventInfo方法来配置Notification在扩展的状态窗口中的外观。扩展的状态窗口将显示图标和在构造函数中传入的时间,以及显示标题和一个详细的字符串。Notification一般表示为对一个动作的请求或引起用户的注意,所以,当用户点击Notification项目时你可以指定一个PendingIntent来触发。
  
  下面的代码片段使用了setLastestEventInfo来设置这些值:
  
  Context context = getApplicationContext();
  
  // Text to display in the extended status window
  String expandedText = “Extended status text”;
  
  // Title for the expanded status
  String expandedTitle = “Notification Title”;
  
  // Intent to launch an activity when the extended text is clicked
  Intent intent = new Intent(this, MyActivity.class);
  PendingIntent launchIntent = PendingIntent.getActivity(context, 0, intent, 0);
  notification.setLatestEventInfo(context,
expandedTitle,
expandedText,
launchIntent);
  
  一个好的形式是显示相同事件(例如,接 收多个SMS消息)的多个对象时 使用一个Notification图 标。为了呈现给用户,使用setLastestEventInfo更新数据集来呈现最近的消息以及重新触发Notification来更新它的值。
  
  你还可以使用number属性来显示一个状态条图标所表示的事件数目。
  
  设置为比1大的数,如下所示,将在状态条图标上以一个小小的数字进行 显示:
  
  notification.number++;
  
  任何对Notification的变更,你都需要重新触发来进行更 新。如果要删除这个数字,设置number的值为0或者-1。
  
  最后,你可以对Notification对象使用多种属性来增强Notification的效果,如闪烁LED、震动电话和播放音乐文件。这些高级特征将在本章的后面部分进行详细描述。
  
  触发Notification
  
  为了触发一个Notification,使用NotificationManager的notify方法,将一个整数的ID和Notification对象传入,如下的片段所示:
  
  int notificationRef = 1;
  notificationManager.notify(notificationRef, notification);
  
  为了更新一个已经触发过的Notification,传入相同的ID。你既可以传入相同的Notification对象,也可以是一个全新的对象。只 要ID相同,新的Notification对象会替换状态条
图标和扩展的状态窗口的细节。
  
  你还可以使用ID来取消Notification,通过调用NotificationManager的cancel方法,如下所示:
  
  notificationManager.cancel(notificationRef);
  
  取消一个Notification时,将移除它的状态条图标以及清除 在扩展的状态窗口中的信息。

Notification和NotificationManager的基本使用方法

1. NotificationManager和Notification用来设置通知。

通知的设置等操作相对比较简单,基本的使用方式就是用新建一个Notification对象,然后设置好通知的各项参数,然后使用系统后台运行的 NotificationManager服务将通知发出来。

基本步骤如下:

1)得到NotificationManager:

String ns = Context.NOTIFICATION_SERVICE;

NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

2)创建一个新的Notification对象:

Notification notification = new Notification();

notification.icon = R.drawable.notification_icon;

也可以使用稍微复杂一些的方式创建Notification:

int icon = R.drawable.notification_icon; //通知图标

CharSequence tickerText = "Hello";  //状态栏(Status Bar)显示的通知文本提示

long when = System.currentTimeMillis(); //通知产生的时间,会在通知信息里显示

Notification notification = new Notification(icon, tickerText, when);

3)填充Notification的各个属性:

Context context = getApplicationContext();

CharSequence contentTitle = "My notification";

CharSequence contentText = "Hello World!";

Intent notificationIntent = new Intent(this, MyClass.class);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

Notification提供了丰富的手机提示方式:

a)在状态栏(Status Bar)显示的通知文本提示,如:

notification.tickerText = "hello";

b)发出提示音,如:

notification.defaults |= Notification.DEFAULT_SOUND;

notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");

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

c)手机振动,如:

notification.defaults |= Notification.DEFAULT_VIBRATE;

long[] vibrate = {0,100,200,300};

notification.vibrate = vibrate;

d)LED灯闪烁,如:

notification.defaults |= Notification.DEFAULT_LIGHTS;

notification.ledARGB = 0xff00ff00;

notification.ledOnMS = 300;

notification.ledOffMS = 1000;

notification.flags |= Notification.FLAG_SHOW_LIGHTS;

4)发送通知:

private static final int ID_NOTIFICATION = 1;

mNotificationManager.notify(ID_NOTIFICATION, notification);

2. 通知的更新

如果需要更新一个通知,只需要在设置好notification之后,再调用setLatestEventInfo,然后重新发送一次通知即可

NotificationManager notiManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

上面这一句,从系统中获得Notification服务,getSystemService()就是这么用,来获得系统服务的。

Notification notification = new Notification(R.drawable.notiicon, msg.getTitle(), System.currentTimeMillis());notification.flags = Notification.FLAG_AUTO_CANCEL;

然后是构造一个Notification,包括三个属性,图标,图标后面的文字,以及Notification时间部分显示出来的时间,通常使用当前时间。FLAG_AUTO_CANCEL说明Notification点击一次就消失。

Intent intent = new Intent(this, MainActivity.class);Bundle bundle = new Bundle();bundle.putString("info", msg.getInfo());intent.putExtras(bundle);intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NEW_TASK);

上面这部分Code,构造一个Intent,并且放进去一条信息。 FLAG_ACTIVITY_CLEAR_TOP, FLAG_ACTIVITY_NEW_TASK者两个FLAG表示优先寻找已经打开的应用,如果应用没有打开那么启动它。

PendingIntent contentIntent = PendingIntent.getActivity(this, id,intent, PendingIntent.FLAG_UPDATE_CURRENT);

这一句代码把Intent包装在PendingIntent里,this是Context,id是PendingIntent的标志,如果id相同会被认为是一个。FLAG_UPDATE_CURRENT是指后来的PendingIntent会更新前面的。

notification.setLatestEventInfo(this, msg.getTitle(), msg.getInfo(),contentIntent);notiManager.notify(id, notification);

最后这两行添加状态栏的详细信息,包装PendingIntent给Notification,最后发送Notification。

.Android Notification 基础相关推荐

  1. android培训总结范文,android培训基础知识总结

    android培训基础知识总结,有需要的朋友可以参考下. android培训之1. Android的四大组件是哪些,它们的作用? Activity:Activity是Android程序与用户交互的窗口 ...

  2. 2017-2018-2 20165236 实验四《Android开发基础》实验报告

    2017-2018-2 20165236 实验四<Android开发基础>实验报告 一.实验报告封面 课程:Java程序设计       班级:1652班       姓名:郭金涛     ...

  3. Android NDK基础样例

    Android NDK基础样例 NDK(Native Development Kit),用C/C++封装一些东西?好像就这么理解好了== 一.环境准备 这个好讨厌==!因为我环境都已经搭了很久了. 已 ...

  4. Android零基础入门第11节:简单几步带你飞,运行Android Studio工程

    2019独角兽企业重金招聘Python工程师标准>>> 之前讲过Eclipse环境下的Android虚拟设备的创建和使用,现在既然升级了Android Studio开发工具,那么对应 ...

  5. Android零基础入门第30节:两分钟掌握FrameLayout帧布局

    原文:Android零基础入门第30节:两分钟掌握FrameLayout帧布局 前面学习了线性布局.相对布局.表格布局,那么本期来学习第四种布局--FrameLayout帧布局. 一.认识FrameL ...

  6. Android零基础入门第25节:最简单最常用的LinearLayout线性布局

    原文:Android零基础入门第25节:最简单最常用的LinearLayout线性布局 良好的布局设计对于UI界面至关重要,在前面也简单介绍过,目前Android中的布局主要有6种,创建的布局文件默认 ...

  7. qtdesigner怎么实现菜单栏跳转_人人都可写代码-Android零基础编程-app 入口菜单栏实操08...

    欢迎来到人人都可写代码,大家好,我是杨晓华,今天我们的课程内容是项目实操,以智者精选项目为例,编写一个Android app的入口关联菜单栏逻辑. 这是要实现的菜单栏组件展示效果,下面就是教大家如何制 ...

  8. Android零基础入门第44节:ListView数据动态更新

    2019独角兽企业重金招聘Python工程师标准>>> 经过前面几期的学习,关于ListView的一些基本用法大概学的差不多了,但是你可能发现了,所有ListView里面要填充的数据 ...

  9. Android零基础入门第38节:初识Adapter

    2019独角兽企业重金招聘Python工程师标准>>> 在上一节一起了解了ListView的简单使用,那么本节继续来学习与ListView有着千丝万缕的Adapter. 一.了解MV ...

最新文章

  1. AI帮你靠“想象”打字:手机电脑软键盘也能盲打了,准确率能达到95%
  2. PLSQL的截取函数
  3. Docker (3)核心概念
  4. 小程序 iphone和安卓_如何阻止iPhone和iPad应用程序要求评级
  5. java中兴参与实参相同_中兴通讯_传输SDH试题(含答案)
  6. 彻底弄懂计算机中的大端小端
  7. linux查看php执行用户,在浏览器中打开php文件时,是Linux中的哪个用户执行的?...
  8. c#利用泛型集合,为自己偷偷懒。
  9. 爬取某类网站并生成csv文件(人民邮电出版社书籍信息)
  10. 《月满西楼》——李清照
  11. Encoded password does not look like BCrypt 异常问题
  12. kettle web 版本 (webspoon) 中文部署 kettle 页面编辑 kettleweb 中文
  13. 巴菲特午餐取消,吃饭行情一地鸡毛
  14. 一周刷爆LeetCode,算法da神左神(左程云)耗时100天打造算法与数据结构基础到高级全家桶教程,直击BTAJ等一线大厂必问算法面试题真题详解 笔记
  15. word2vec 词向量
  16. android升级刷机,安卓系统怎么升级 怎么刷机安卓系统
  17. 如何用MathType编辑化学等式
  18. 汇编语言-第三版-王爽-实验6、7、9、10、11、12、13、14、15
  19. 扁平化简洁工作计划安排PPT模板
  20. 华为荣耀七刷机后显示无服务器,手机刷机成砖怎么办?华为荣耀7刷机四个须知...

热门文章

  1. 搜索引擎知识图谱相关结构化数据挖掘与去歧处理
  2. 华为平板2鸿蒙,华为 MatePad Pro 2 平板电脑入网:首款预装鸿蒙 OS
  3. 设计师研发远程无线充电器NIC,隔空5米也可以自动充电...
  4. 单体框架和分布式框架的区别
  5. 串口中继器是什么应用在那些领域
  6. 使用矩形选框工具制作相框
  7. 王者荣耀s16服务器维护,王者荣耀S16兵线刷新时间表一览
  8. iphone11各机型对比_iPhone11和iPhone12系列对比:新款有何改变?
  9. 网络安全原来有这么多大厂,码住!
  10. pip 安装.whl文件