大家都熟悉这样的一个场景:就是来短信的时候,手机上方会跳出一个短信的图标来提示你来新的信息了,然后你在上方拖下来就会看到短信息,点进去之后就能进到阅读短信的页面。这个流程一整套的完成就是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);
}
}
NotifytestActivity2.java
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的功劳哦)