<1>简介
Android系统的状态栏(Status Bar)中有一个创新UI设计,这就是可以下拉的通知提示。当系统有一些消息要通知用户时,例如,收到短信、电子邮件、有未接来电时,都会把信息作为通知(Notification)发送给用户。
Status Bar 增加了一个图标到系统状态栏中,还有文本信息(可以不选),增加Notification信息到Notification窗口。你还可以安装Notification利用的声音,震动,设备上闪关灯来提醒用户。
Notification与Toast都可以起到通知、提醒的作用。但它们的实现原理和表现形式却完全不一样。
Toast其实相当于一个组件(Widget)。有些类似于没有按钮的对话框。而Notification是显示在屏幕上方状态栏中的信息。
Notification需要用NotificationManager来管理,而Toast只需要简单地创建Toast对象即可。
<2>步骤:
创建 status bar notification:
1。NotificationManager得到一个参考:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
或者
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
2。创建一个Notification对象。每一个Notification对应一个Notification对象。在这一步需要设置显示在屏幕上方状态栏的通知消息、通知消息前方的图像资源ID和发出通知的时间。一般为当前时间。
int icon = R.drawable.notification_icon; //显示在屏幕上方状态栏的图标
CharSequence tickerText = "Hello"; //显示在屏幕上方状态栏的通知消息
long when = System.currentTimeMillis(); //发出通知的时间
Notification notification = new Notification(icon, tickerText, when);
3。定义的信息和PendingIntent通知:
由于Notification可以与应用程序脱离。也就是说,即使应用程序被关闭,Notification仍然会显示在状态栏中。当应用程序再次启动后,又可以重新控制这些Notification。如清除或替换它们。因此,需要创建一个PendingIntent对象。该对象由Android系统负责维护,因此,在应用程序关闭后,该对象仍然不会被释放。
使用Notification类的setLatestEventInfo方法设置Notification的详细信息。
Context context = getApplicationContext();
CharSequence contentTitle = "My notification"; //Notification通知标题
CharSequence contentText = "Hello World!"; //Notification通知内容
Intent notificationIntent = new Intent(this, MyClass.class); //跳转到MyClass.class
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); //生成一个PendingIntent
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); //很重要的方法
4。通过对NotificationManager通知:
使用NotificationManager类的notify方法显示Notification消息。在这一步需要指定标识Notification的唯一ID。这个ID必须相对于同一个NotificationManager对象是唯一的,否则就会覆盖相同ID的Notificaiton()
private static final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
在这里要解释一下的是notify方法的第1个参数。这个参数实际上表示了Notification的ID。是一个int类型的值。
为了使这个值唯一,可以使用res目录中的某些资源ID。例如,在上面的代码中使用了当前Notification显示的图像对应的资源ID(R.drawable.icon)作为Notification的ID。当然,读者也可以使用其他的值作为Notification的ID值。
5。在显示Notification时还可以设置显示通知时的默认发声、震动和Light效果。要实现这个功能需要设置Notification类的defaults属性,代码如下:
1 添加声音:
你可以提醒用户提供了默认的通知声音(这是由用户定义)或应用程序特定的声音
应用程序规定的声音:
notification.defaults = Notification.DEFAULT_SOUND;
自定义:使用一个不同的声音的Notification,通过一个Uri引用声音的位置。下面的例子使用一个保存到装置的SD卡已知的音频文件:
notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
2 增加振动:
你可以提醒用户提供了震动(这是由用户定义)或应用程序特定的振动模式
应用程序规定的振动模式:
notification.defaults = Notification.DEFAULT_VIBRATE;
定义你自己的振动模式,通过一系列的长值:
long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;
第一个值是等待要花费你多长时间(关闭)开始之前,第二个值是第一个振动的长度,三是下一个长度时,等等。花纹可只要你喜欢,但它不能被设定为重复。
3添加灯光闪烁
通知用户发出闪光LED灯,你可以继承默认t模式(如有),或定义自己的颜色和图案的灯。
使用默认的灯光设置,加上DEFAULT_LIGHTS到defaults:
notification.defaults = Notification.DEFAULT_LIGHTS;
<3>更多功能 (利用 Flag)
notification.flags = Notification.FLAG_AUTO_CANCEL;
自动取消通知之后,这是选择通知窗口。
notification.flags = Notification.
FLAG_INSISTENT
;
重复的声响,直到用户响应。
FLAG_NO_CLEAR;notification.flags = Notification.
表明通知不应该被“Clear notifications”按钮清除。这是特别有用,如果你的notification正在通知。
范例:
package xiaosi.notification;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
public class NotificationActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.a,"你有短消息了",System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent();
PendingIntent pedingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, "你有短消息了", "祝你新婚快乐", pedingIntent);
notificationManager.notify(R.drawable.a,notification);
}
}
package xiaosi.notification;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Notification演示"/>
</LinearLayout>
点击下载源码:源代码