状态栏通知的创建

  正如我们非常常见的,在使用android手机时,会有许多通知(notification),如短信,一些后台任务的信息。当然我们可以为自己的应用注册notification,用声音,震动,或是LED灯

提示用户相关事件的发生! 下面就让我们开始吧。

   为了创建一个notification,你必须使用两个类:Notification and NotificationManager

  Notification 通过实例化一个Notification,你可以定义你自己Notification的属性,如icon、message、或者是一段音频,这和我们常见的控件或是其他组件的定义类似

  NotificationManager 它是一个android系统级的服务,负责执行和管理所有的notification. 为了使用它我们需要调用 getSystemService()获得它的一个引用。

   下面是详细代码及解释:

import android.content.Intent;
import android.os.Bundle;

public class MyNotificationActivity extends Activity {
private static final int HELLO_ID = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//通过getSystemService获得NotificationManager的引用


String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
//实例化你的Notification

int icon = R.drawable.icon;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();

Notification notification = new Notification(icon,tickerText,when);
notification.defaults |= Notification.DEFAULT_SOUND;//添加一个sound


//定义通知的消息和意图

Context context = getApplicationContext();
CharSequence contentTitle = "My notification ~~";//message title
CharSequence contentText = "Hello World !"; //message text
Intent notificationIntent = new Intent(this,MyNotificationActivity.class);


//当该notification被点击后,会触发该intent

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

//将该notification传递给NotificationManager
mNotificationManager.notify(HELLO_ID, notification);


}


为了对每个notification进行更新或者管理,每一个notification都需要一个唯一的ID, 如该例子中的"HELLO_ID". 你也可能会需要去更新notification中显示的文本内容,这是您可以通过调用 setLatestEventInfo() 然后再次调用notify()来达到目的,事实上该方法已经不推荐使用,取代它的是Notification.Builder,使用该builder对像可以很容易的对notification的属性状态进行更新,详细用法可以查看帮助文档,下面截图是该程序代码的执行结果。

 

除过上面直接定义系统的Notification外,我们还可以自定义自己的Notification样式,请看下面这个例子:

这是官方文档上的一个例子布局: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">
</textview></imageview></linearlayout>

 这里我们需要用的RemoteViews ,代码如下:

public class MyNotificationActivity extends Activity {
// private static final int HELLO_ID = 1;
private static final int CUSTOM_VIEW_ID = 2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//通过getSystemService获得NotificationManager的引用
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
//实例化你的Notification
int icon = R.drawable.icon;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();

Notification notification = new Notification(icon,tickerText,when);
Intent notificationIntent = new Intent(this,MyNotificationActivity.class);
//当该notification被点击后,会触发该intent
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

<span style="color:#006600;"> RemoteViews contentView = new RemoteViews (getPackageName(),R.layout.custom_notification_layout);
contentView.setImageViewResource(R.id.image, android.R.drawable.ic_notification_overlay);
contentView.setTextViewText(R.id.text, "Hello, this message is in a custom expanded view");
notification.contentView = contentView;
notification.contentIntent = contentIntent;</span>
mNotificationManager.notify(CUSTOM_VIEW_ID, notification);

}
}



图标太扯了~~,就写到这吧,更多详细还是参见官方文档吧!
posted @ 2011-12-09 18:36  zsmiling  阅读(415)  评论(0编辑  收藏  举报