一般来说,状态栏的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);
本文介绍如何使用NotificationManager和Notification类创建Android状态栏通知。通过设置图标、文本、时间和内容等属性,实现自定义通知并显示在状态栏。
3897

被折叠的 条评论
为什么被折叠?



