大家都熟悉这样的一个场景:就是来短信的时候,手机上方会跳出一个短信的图标来提示你来新的信息了,然后你在上方拖下来就会看到短信息,点进去之后就能进到阅读短信的页面。这个流程一整套的完成就是android中的notify机制,下面我们一起来看看android中的notify机制,主要包含三个类:

   1. NotificationManager: 

                                           通知管理器,我们就理解其实一个通知消息的管理者就可以了,整个系统就一个,通过getSystemService()函数来获得,它负责管理发送通知、清除通知等一系列的对通知的管理工作。

    2.Notification :

                               通知本身,可以设置通知的铃声,震动方式等,通知点进去之后需要启动的应用(通过PendingIntent传递)

    3:PendingIntent:

                              字面意思上来理解的话时悬挂起来的intent,intent的代表意图,这个叫悬起来的意图,解释不通。我的理解是这样:PendingIntent主要是为了授予其它的应用启动某个activity的权利,比如说:在短信程序里面收到的消息会发出一个通知,在通知栏里面你点击的时候会启动阅读短信的那个view(一般情况下是不行的,需要通过startActiviy等方式来做,这里是不需要的),怎么做到的呢?就是PendingIntent做到得,因为他在短信程序中利用PendingIntent方式赋予了再通知栏中点击启动短信view的权限。

下面来看代码:

NotifytestActivity.java

[java]  view plain copy
  1. package com.huawei.android.notify_1;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.Button;  
  6. import android.app.NotificationManager;  
  7. public class NotifytestActivity extends Activity {  
  8.     /** Called when the activity is first created. */  
  9.     @Override  
  10.     public void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.main2);  
  13.     }  
  14.     /** 
  15.      * 启动该activity之后就应该清除通知标记 
  16.      */  
  17.     @Override  
  18.     protected void onStart() {  
  19.         // TODO Auto-generated method stub  
  20.         super.onStart();  
  21.         NotificationManager nNotificaitonMan = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
  22.         nNotificaitonMan.cancel(0);  
  23.     }  
  24.       
  25. }  
NotifytestActivity2.java

[java]  view plain copy
  1. package com.huawei.android.notify_1;  
  2.   
  3. import android.app.Activity;  
  4. import android.view.View;  
  5. import android.widget.Button;  
  6. import android.os.Bundle;  
  7. import android.app.Notification;  
  8. import android.app.NotificationManager;  
  9. import android.app.PendingIntent;  
  10. import android.content.Intent;  
  11.   
  12. public class NotifytestActivity2 extends Activity {  
  13.   
  14.     private Button mButton1 = null;  
  15.     private Button mButton2 = null;  
  16.     //声明消息管理器  
  17.     NotificationManager mNotifyManager = null;  
  18.     Intent mIntent = null;  
  19.     PendingIntent mPendIntent = null;  
  20.     //声明notify对象  
  21.     Notification mNotify = null;  
  22.     @Override  
  23.     protected void onCreate(Bundle savedInstanceState) {  
  24.         // TODO Auto-generated method stub  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.main);  
  27.           
  28.         //初始化notification 对象  
  29.         mNotifyManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);  
  30.           
  31.         //获取4个按钮对象  
  32.         mButton1 = (Button)findViewById(R.id.btn_1);  
  33.         mButton2 = (Button)findViewById(R.id.btn_2);  
  34.         //当点击的时候转移内容  
  35.         mIntent = new Intent(NotifytestActivity2.this,NotifytestActivity.class);  
  36.           
  37.         //设置点击时候显示内容的类,  
  38.         mPendIntent = PendingIntent.getActivity(NotifytestActivity2.this,0,mIntent,0);  
  39.           
  40.         //构造notification对象  
  41.         mNotify = new Notification();  
  42.           
  43.         mButton1.setOnClickListener(new Button.OnClickListener(){  
  44.               
  45.             public void onClick(View v){  
  46.                   
  47.                 //设置通知在状态栏显示的图标  
  48.                 mNotify.icon = R.drawable.icon;  
  49.                 //当我们点击通知时显示的内容  
  50.                 mNotify.tickerText = "来新的通知啦~~~";  
  51.                 //通知时发出的声音  
  52.                 mNotify.defaults = Notification.DEFAULT_SOUND;  
  53.                 //设置通知显示的参数  
  54.                 mNotify.setLatestEventInfo(NotifytestActivity2.this"Button1""Button1通知进行中", mPendIntent);  
  55.                   
  56.                 //执行这个通知事件的跳转  
  57.                 mNotifyManager.notify(0, mNotify);  
  58.                   
  59.             }  
  60.   
  61.         });  
  62.         /** 
  63.          * 清楚通知,mNotifyManager.cancel(0)的参数0是mNotifyManager.notify(0, mNotify);里面第一个参数,也就是notify的ID,这个在系统中是唯一的。 
  64.          * 这里是做测试用的,在系统中应该是点击了通知之后该通知图标就消失了。可以看NotifytestActivity中的onStart()中的处理方式。 
  65.          */  
  66.         mButton2.setOnClickListener(new Button.OnClickListener(){  
  67.           
  68.             public void onClick(View v){  
  69.                 mNotifyManager.cancel(0);  
  70.             }  
  71.         });  
  72.           
  73.     }  
  74.   
  75.       
  76. }  

main.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/hello"  
  11.     />  
  12. <Button   
  13.     android:id="@+id/btn_1"  
  14.     android:layout_width="150px"  
  15.     android:layout_height="wrap_content"  
  16.     android:text="@string/button_1"  
  17. />  
  18. <Button   
  19.     android:id="@+id/btn_2"  
  20.     android:layout_width="150px"  
  21.     android:layout_height="wrap_content"  
  22.     android:text="@string/button_2"  
  23. />  
  24. LinearLayout>  

main2.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/hello"  
  11.     />  
  12.   
  13. LinearLayout>  

运行结果如下:

1.点击发送通知之后:



按下上方的状态栏拖下:

3.点击该通知后(从该通知进入到启动的那个activity是PendingIntent的功劳哦)



更多相关文章

  1. 【Android】学习笔记(5)——浅谈Handler
  2. 探索Android中的Parcel机制(上) .
  3. Android(安卓)ActivityManagerService(AMS)的启动分析
  4. GC机制,你真的了解吗?
  5. Android的消息机制之ThreadLocal的工作原理
  6. Android--关于Cursor空指针的问题
  7. Android(安卓)UI学习 - 用户通知
  8. Android开发之Serializable 和 Parcelable的区别(源代码分享)
  9. Android中实现Bitmap在自定义View中的放大与拖动

随机推荐

  1. Android--About Android(欢迎修改、补充)
  2. 《阿里巴巴Android开发手册》正式发布,献
  3. 创建Android虚拟模拟器
  4. Android Studio 快捷键整理分享-SadieYu
  5. android得到清单文件里meta标签的属性值
  6. android SDK开发环境搭建(Android 4.0.3
  7. Android Fragment 深入理解
  8. SONY 系列手机 Android 5.1 系统 Root 方
  9. Bundle源码解析
  10. ffmpeg入门教程之Android使用FFmpeg so(AP