Android之基础Notification

一般来说,状态栏的Notification用来通知用户后台Service操作执行的情况

要创建一个状态栏Notification你需要用到NotificationManager和Notification类

1.获得NotificationManager类

NotificationManager nm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

2.实例化Notification类

int icon=R.drawable.notification_icon;

CharSequence text="new Message";

long when=System.currentTimeMillis();

Notification notification=new Notification(icon,text,when);

3.下拉状态栏看到的通知的内容及相应动作

CharSequence contentTitle="Friend's name";

CharSequence contentText="how's it doing there?"

Intent notificationIntent=new Intent(this,SomeActivity.class);

PendingIntent contentIntent=PendingIntent.getActivity(this,0,notificationIntent,0);


notification.setLatestInfo(getApplicationContext(),contentTitle,contentText,contentIntent);

4.把notification加入到notificationManager

private static final int TEST_NOTI=1;

nm.notify(TEST_NOTI,notification);


posted @ 2012-02-29 22:10  nightkidzxc  阅读(212)  评论(0)    收藏  举报