android之Notification通知

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

 

[java] view plain copyandroid

  1. package cn.com.chenzheng_java;  
  2.   
  3. import <a href="http://lib.csdn.net/base/android" class='replace_word' title="Android知识库" target='_blank' style='color:#df3434; font-weight:bold;'>Android</a>.app.Activity;  
  4. import android.app.Notification;  
  5. import android.app.NotificationManager;  
  6. import android.app.PendingIntent;  
  7. import android.content.Context;  
  8. import android.content.Intent;  
  9. import android<a href="http://lib.csdn.net/base/dotnet" class='replace_word' title=".NET知识库" target='_blank' style='color:#df3434; font-weight:bold;'>.NET</a>.Uri;  
  10. import android.os.Bundle;  
  11. import android.provider.MediaStore.Audio;  
  12. import android.view.View;  
  13. import android.widget.Button;  
  14.   
  15. /*** 
  16.  * @description 状态栏通知相关 
  17.  * @author chenzheng_java 
  18.  *  
  19.  */  
  20. public class NotificationActivity extends Activity {  
  21.     @Override  
  22.     protected void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.notification);  
  25.   
  26.         Button button = (Button) findViewById(R.id.button);  
  27.         button.setOnClickListener(new View.OnClickListener() {  
  28.   
  29.             @Override  
  30.             public void onClick(View v) {  
  31.                 addNotificaction();  
  32.   
  33.             }  
  34.         });  
  35.   
  36.     }  
  37.       
  38.       
  39.       
  40.   
  41.     /** 
  42.      * 添加一个notification 
  43.      */  
  44.     private void addNotificaction() {  
  45.         NotificationManager manager = (NotificationManager) this  
  46.         .getSystemService(Context.NOTIFICATION_SERVICE);  
  47.         // 建立一个Notification  
  48.         Notification notification = new Notification();  
  49.         // 设置显示在手机最上边的状态栏的图标  
  50.         notification.icon = R.drawable.excel;  
  51.         // 当当前的notification被放到状态栏上的时候,提示内容  
  52.         notification.tickerText = "注意了,我被扔到状态栏了";  
  53.           
  54.         /*** 
  55.          * notification.contentIntent:一个PendingIntent对象,当用户点击了状态栏上的图标时,该Intent会被触发 
  56.          * notification.contentView:咱们能够不在状态栏放图标而是放一个view 
  57.          * notification.deleteIntent 当当前notification被移除时执行的intent 
  58.          * notification.vibrate 当手机震动时,震动周期设置 
  59.          */  
  60.         // 添加声音提示  
  61.         notification.defaults=Notification.DEFAULT_SOUND;  
  62.         // audioStreamType的值必须AudioManager中的值,表明着响铃的模式  
  63.         notification.audioStreamType= android.media.AudioManager.ADJUST_LOWER;  
  64.           
  65.         //下边的两个方式能够添加音乐  
  66.         //notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");   
  67.         //notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");   
  68.         Intent intent = new Intent(this, Notification2Activity.class);  
  69.         PendingIntent pendingIntent = PendingIntent.getActivity(this0, intent, PendingIntent.FLAG_ONE_SHOT);  
  70.         // 点击状态栏的图标出现的提示信息设置  
  71.         notification.setLatestEventInfo(this"内容提示:""我就是一个<a href="http://lib.csdn.net/base/softwaretest" class='replace_word' title="软件测试知识库" target='_blank' style='color:#df3434; font-weight:bold;'>测试</a>文件", pendingIntent);  
  72.         manager.notify(1, notification);  
  73.           
  74.     }  
  75.   
  76. }  

 

点击按钮时候,状态栏会显示:app

看到了吧,状态栏多出来一个excel图标,当我按住图标不放,往下拖动的时候,出来了这个页面ide

而后,当咱们点击这个对话框以后,就会触发intent,跳转到Notification2Activity.Java这个activity。测试

----------------------------------------------------------------------------------------this

注意,NotificationManager里的notify(id,notification)中的id是用来惟一标识咱们当前的这个notification的标识符,咱们经过cancel方法删除通知时,传递的就是这个值。可能读者在看不少文档的时候,发现这个地方指定了一个莫名奇妙的值,例如R.drawable.icon,不少朋友就纳闷了,为何这里要指定一个图片呢。这里笔者就介绍下,为何呢?spa

 答案其实很简单,咱们都知道,咱们这里对参数的惟一要求就是,这个id要和notify方法中的一致,而且是惟一;只要知足了这两项,其余的都无所谓。notify和cancel里一致咱们做为开发者,太好控制了,可是惟一呢,咱们还真很差说,因而这里就有些人动小脑筋了,很巧妙的用了咱们系统中的图片资源或者其余资源的索引ID,咱们都知道,这些值确定都是惟一的!.net

------------------------------------------------------------------------------------------excel

下面是从网上找的一些资料:对象

 

  若是要添加一个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同样。

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

 

------------------------------------------------------------------------------------------------------

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提供了丰富的手机提示方式:

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);

相关文章
相关标签/搜索