大家都熟悉这样的一个场景:就是来短信的时候,手机上方会跳出一个短信的图标来提示你来新的信息了,然后你在上方拖下来就会看到短信息,点进去之后就能进到阅读短信的页面。这个流程一整套的完成就是android中的notify机制,下面我们一起来看看android中的notify机制,主要包含三个类:
1. NotificationManager:
通知管理器,我们就理解其实一个通知消息的管理者就可以了,整个系统就一个,通过getSystemService()函数来获得,它负责管理发送通知、清除通知等一系列的对通知的管理工作。
2.Notification :
通知本身,可以设置通知的铃声,震动方式等,通知点进去之后需要启动的应用(通过PendingIntent传递)
3:PendingIntent:
字面意思上来理解的话时悬挂起来的intent,intent的代表意图,这个叫悬起来的意图,解释不通。我的理解是这样:PendingIntent主要是为了授予其它的应用启动某个activity的权利,比如说:在短信程序里面收到的消息会发出一个通知,在通知栏里面你点击的时候会启动阅读短信的那个view(一般情况下是不行的,需要通过startActiviy等方式来做,这里是不需要的),怎么做到的呢?就是PendingIntent做到得,因为他在短信程序中利用PendingIntent方式赋予了再通知栏中点击启动短信view的权限。
下面来看代码:
NotifytestActivity.java
- package com.huawei.android.notify_1;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.Button;
- import android.app.NotificationManager;
- public class NotifytestActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main2);
- }
- /**
- * 启动该activity之后就应该清除通知标记
- */
- @Override
- protected void onStart() {
- // TODO Auto-generated method stub
- super.onStart();
- NotificationManager nNotificaitonMan = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- nNotificaitonMan.cancel(0);
- }
- }
- package com.huawei.android.notify_1;
- import android.app.Activity;
- import android.view.View;
- import android.widget.Button;
- import android.os.Bundle;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Intent;
- public class NotifytestActivity2 extends Activity {
- private Button mButton1 = null;
- private Button mButton2 = null;
- //声明消息管理器
- NotificationManager mNotifyManager = null;
- Intent mIntent = null;
- PendingIntent mPendIntent = null;
- //声明notify对象
- Notification mNotify = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //初始化notification 对象
- mNotifyManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
- //获取4个按钮对象
- mButton1 = (Button)findViewById(R.id.btn_1);
- mButton2 = (Button)findViewById(R.id.btn_2);
- //当点击的时候转移内容
- mIntent = new Intent(NotifytestActivity2.this,NotifytestActivity.class);
- //设置点击时候显示内容的类,
- mPendIntent = PendingIntent.getActivity(NotifytestActivity2.this,0,mIntent,0);
- //构造notification对象
- mNotify = new Notification();
- mButton1.setOnClickListener(new Button.OnClickListener(){
- public void onClick(View v){
- //设置通知在状态栏显示的图标
- mNotify.icon = R.drawable.icon;
- //当我们点击通知时显示的内容
- mNotify.tickerText = "来新的通知啦~~~";
- //通知时发出的声音
- mNotify.defaults = Notification.DEFAULT_SOUND;
- //设置通知显示的参数
- mNotify.setLatestEventInfo(NotifytestActivity2.this, "Button1", "Button1通知进行中", mPendIntent);
- //执行这个通知事件的跳转
- mNotifyManager.notify(0, mNotify);
- }
- });
- /**
- * 清楚通知,mNotifyManager.cancel(0)的参数0是mNotifyManager.notify(0, mNotify);里面第一个参数,也就是notify的ID,这个在系统中是唯一的。
- * 这里是做测试用的,在系统中应该是点击了通知之后该通知图标就消失了。可以看NotifytestActivity中的onStart()中的处理方式。
- */
- mButton2.setOnClickListener(new Button.OnClickListener(){
- public void onClick(View v){
- mNotifyManager.cancel(0);
- }
- });
- }
- }
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- <Button
- android:id="@+id/btn_1"
- android:layout_width="150px"
- android:layout_height="wrap_content"
- android:text="@string/button_1"
- />
- <Button
- android:id="@+id/btn_2"
- android:layout_width="150px"
- android:layout_height="wrap_content"
- android:text="@string/button_2"
- />
- </LinearLayout>
main2.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- </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作为参数。
主要的使用的地方和例子:通知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和当前Application的Context。正由于PendingIntent中保存有当前Application的Context,使它赋予带他程序一种执行的Intent的能力,就算在执行时当前Application已经不存在了,也能通过存在PendingIntent里的Context照样执行Intent。
PendingIntent的一个很好的例子:
SmsManager的用于发送短信的方法:
sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent);
第一个参数:destinationAddress 对方手机号码
第二个参数:scAddress 短信中心号码 一般设置为空
第三个参数:text 短信内容
第四个参数:sentIntent判断短信是否发送成功,如果你没有SIM卡,或者网络中断,则可以通过这个itent来判断。注意强调的是“发送”的动作是否成功。那么至于对于对方是否收到,另当别论
第五个参数:deliveryIntent当短信发送到收件人时,会收到这个deliveryIntent。即强调了“发送”后的结果
就是说是在"短信发送成功"和"对方收到此短信"才会激活 sentIntent和deliveryIntent这两个Intent。这也相当于是延迟执行了Intent
上面两个例子可以理解,PendingIntent就是一个可以在满足一定条件下执行的Intent,它相比于Intent的优势在于自己携带有Context对象,这样他就不必依赖于某个activity才可以存在。