Android 中的notify 机制

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

   1. NotificationManager: 

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

    2.Notification :

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

    3:PendingIntent:

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

下面来看代码:

NotifytestActivity.java

  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
  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
  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
  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的功劳哦)


关于PendingIntent的使用,下面有更详细的解释:

pendingIntent字面意义:等待的,未决定的Intent。
要得到一个pendingIntent对象,使用方法类的静态方法 getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), getService(Context, int, Intent, int)  分别对应着Intent的3个行为,跳转到一个activity组件、打开一个广播组件和打开一个服务组件。
参数有4个,比较重要的事第三个和第一个,其次是第四个和第二个。可以看到,要得到这个对象,必须传入一个Intent作为参数,必须有context作为参数。

pendingIntent是一种特殊的Intent。主要的区别在于Intent的执行立刻的,而pendingIntent的执行不是立刻的。pendingIntent执行的操作实质上是参数传进来的Intent的操作,但是使用pendingIntent的目的在于它所包含的Intent的操作的执行是需要满足某些条件的。
主要的使用的地方和例子:通知Notificatio的发送,短消息SmsManager的发送和警报器AlarmManager的执行等等。

Android的状态栏通知(Notification)

如果需要查看消息,可以拖动状态栏到屏幕下方即可查看消息。

步骤:

1 获取通知管理器NotificationManager,它也是一个系统服务

2 建立通知Notification notification = new Notification(icon, null, when);

3 为新通知设置参数(比如声音,震动,灯光闪烁)

4 把新通知添加到通知管理器

发送消息的代码如下:

//获取通知管理器

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)

int icon = android.R.drawable.stat_notify_chat;

long when = System.currentTimeMillis();//通知发生的时间为系统当前时间

//新建一个通知,指定其图标和标题

Notification notification = new Notification(icon, null, when);//第一个参数为图标,第二个参数为短暂提示标题,第三个为通知时间

notification.defaults = Notification.DEFAULT_SOUND;//发出默认声音

notification.flags |= Notification.FLAG_AUTO_CANCEL;//点击通知后自动清除通知

Intent openintent = new Intent(this, OtherActivity.class);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, openintent, 0);//当点击消息时就会向系统发送openintent意图

notification.setLatestEventInfo(this, “标题”, “我是内容", contentIntent);

mNotificationManager.notify(0, notification);//第一个参数为自定义的通知唯一标识

 

重点是setLatestEventInfo( )方法的最后一个参数!!!!它是一个PendingIntent!!!!!!!!!

这里使用到了PendingIntent(pend本意是待定,不确定的意思)

PendingIntent可以看作是对Intent的包装。PendingIntent主要持有的信息是它所包装的Intent和当前ApplicationContext。正由于PendingIntent中保存有当前ApplicationContext,使它赋予带他程序一种执行的Intent的能力,就算在执行时当前Application已经不存在了,也能通过存在PendingIntent里的Context照样执行Intent

 

PendingIntent的一个很好的例子:

SmsManager的用于发送短信的方法:

sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent);

第一个参数:destinationAddress 对方手机号码

第二个参数:scAddress 短信中心号码 一般设置为空

第三个参数:text 短信内容

第四个参数:sentIntent判断短信是否发送成功,如果你没有SIM卡,或者网络中断,则可以通过这个itent来判断。注意强调的是“发送”的动作是否成功。那么至于对于对方是否收到,另当别论

第五个参数:deliveryIntent当短信发送到收件人时,会收到这个deliveryIntent。即强调了“发送”后的结果

就是说是在"短信发送成功""对方收到此短信"才会激活 sentIntentdeliveryIntent这两个Intent。这也相当于是延迟执行了Intent


上面两个例子可以理解,PendingIntent就是一个可以在满足一定条件下执行的Intent,它相比于Intent的优势在于自己携带有Context对象,这样他就不必依赖于某个activity才可以存在。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章