创建状态栏通知
通常一个后台服务运行时,如果需要提醒用户一些事件、或者让用户反馈一些信息时,通常用到状态栏提醒。一个后台Service永远不会自己运行一个activity来接受用户交互,一般的,后台服务会添加一个状态栏通知来与用户进行交互。 下图显示了一个状态栏通知:
接下来的会显示状态栏窗口。用户可以打开这个窗口通过下滑状态栏。或者使用menu菜单键选择。

基础知识 一个activity或者Service可以初始化状态栏通知,因为activity只有在活动状态下才能执行一些命令,所以你需要从一个service来建立状态通知。当用户启动了其他程序或者设备已经休眠时,通过这种方式,通知就可以在后台被创建。你要用到这两个类:Notification类和NotificationManager类。 Notification类来定义状态通知的属性,比如图标,提示信息,或者提示声音。NotificationManager是一个android系统的服务,来管理和运行所有通知的,他不能被实例化,你可以用getSystemService()方法获得他的句柄。当你想通知用户时,调用notify()方法即可。 创建一个菜单栏通知: 1-获得MotificationManager的引用。
- String ns = Context.NOTIFICATION_SERVICE;
- NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
- int icon = R.drawable.notification_icon;
- CharSequence tickerText = "Hello";
- long when = System.currentTimeMillis();
- Notification notification = new Notification(icon, tickerText, when);
- Context context = getApplicationContext();
- CharSequence contentTitle = "My notification";
- CharSequence contentText = "Hello World!";
- Intent notificationIntent = new Intent(this, MyClass.class);
- PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
- notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
- private static final int HELLO_ID = 1;
- mNotificationManager.notify(HELLO_ID, notification);
管理你的通知NotificationManager是一个管理所有通知的系统服务。你可以通过getSystemService()方法得到她的句柄:
- String ns = Context.NOTIFICATION_SERVICE;
- NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
建立一个通知 一个通知对象定义了显示在状态栏的一些细节信息,和所有通知方式,例如声音、闪光。一个状态栏通知必须包括以下几点:@ 显示在状态栏的图标@ 一个标题(除非你自定义了提示界面)@ 一个Pending Intent,即点击后要做的操作。可选项包括以下:@ 状态栏提示文本@ 提示声音@ 震动@ 闪光初学者套件?为新的通知包含了MNotification(int,CharSequence,long)构造方法和setLatestEventInfo(Context,CharSequence,CharSequence,PendingIntent)方法。这些参数是一个通知的必要参数。下面的代码片段是一个简单的例子:
- int icon = R.drawable.notification_icon; // icon from resources
- CharSequence tickerText = "Hello"; // ticker-text
- long when = System.currentTimeMillis(); // notification time
- Context context = getApplicationContext(); // application Context
- CharSequence contentTitle = "My notification"; // expanded message title
- CharSequence contentText = "Hello World!"; // expanded message text
- Intent notificationIntent = new Intent(this, MyClass.class);
- PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
- // the next two lines initialize the Notification, using the configurations above
- Notification notification = new Notification(icon, tickerText, when);
- notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
- notification.defaults |= Notification.DEFAULT_SOUND;
- notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
- notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
- notification.defaults |= Notification.DEFAULT_VIBRATE;
- long[] vibrate = {0,100,200,300};
- notification.vibrate = vibrate;
- notification.defaults |= Notification.DEFAULT_LIGHTS;
- notification.ledARGB = 0xff00ff00;
- notification.ledOnMS = 300;
- notification.ledOffMS = 1000;
- notification.flags |= Notification.FLAG_SHOW_LIGHTS;
建立一个自定义的通知视图

默认情况下,下拉的Notification窗口包含一个标题和文本信息。setLatestEventInfo()有两个默认的参数contentTitle和contentText 。然而你通过RemoteViews也可以定义自己下拉通知视图。上卖弄的截图就显示了一个自定义布局的通知视图,包括一个TextView和ImageView。 定义自己的通知视图,实例化RemoteViews对象并且传递给contentView。给contentIntent字段传递PendingIntent值。创建一个自定义的通知视图最好的理解方法就是写一个例子: 1-建立xml布局文件。例如:custom_notification_layout.xml:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:padding="3dp"
- >
- <ImageView android:id="@+id/image"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_marginRight="10dp"
- />
- <TextView android:id="@+id/text"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:textColor="#000"
- />
- </LinearLayout>
- RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
- contentView.setImageViewResource(R.id.image, R.drawable.notification_image);
- contentView.setTextViewText(R.id.text, "Hello, this message is in a custom expanded view");
- notification.contentView = contentView;
- Intent notificationIntent = new Intent(this, MyClass.class);
- PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
- notification.contentIntent = contentIntent;
- mNotificationManager.notify(CUSTOM_VIEW_ID, notification);

浙公网安备 33010602011771号