1、定义notification,可以放在Activity或Service类中
//NF1:notification 通知
private Notification mNotification;
private NotificationManager mNotificationManager;
private final static int
NOTIFICATION_ID = 0x0001;
//通知的ID
一、标准Notification
1、设置Notification,可以放在Oncreate函数里
//NF2:设置Notification
mNotification = new Notification(R.drawable.icon,"This is a
notification.",System.currentTimeMillis());
//将使用默认的声音来提醒用户
mNotification.defaults = Notification.DEFAULT_SOUND;
mNotificationManager =
(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
2、显示Notification,放在开始显示时的地方
//NF3:显示Notification
Intent mIntent = new
Intent(this,BindServiceActivity.class);//BindServiceActivity为点击该通知后要跳转的类
//这里需要设置Intent.FLAG_ACTIVITY_NEW_TASK属性
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent mContentIntent
=PendingIntent.getActivity(this,0, mIntent, 0);
//这里必需要用setLatestEventInfo(上下文,标题,内容,PendingIntent)不然会报错.
mNotification.setLatestEventInfo(this, "10086", "您的当前话费不足,请充值.哈哈~",
mContentIntent);
//这里发送通知(消息ID,通知对象)
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
3、1、2也可以整合成一个函数
public
void showNotification()
{
mNotification = new Notification(R.drawable.icon,"This
is a notification.",System.currentTimeMillis());
//将使用默认的声音来提醒用户
mNotification.defaults = Notification.DEFAULT_SOUND;
//NF3:显示Notification,设置通知的事件消息
Intent mIntent = new
Intent(this,BindServiceActivity.class);
//BindServiceActivity为点击该通知后要跳转的Activity
//这里需要设置Intent.FLAG_ACTIVITY_NEW_TASK属性
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent
mContentIntent =PendingIntent.getActivity(this,0, mIntent, 0);
//这里必需要用setLatestEventInfo(上下文,标题,内容,PendingIntent)不然会报错.
mNotification.setLatestEventInfo(this, "我的Notification", "后台计数服务已启动~",
mContentIntent);
//这里发送通知(消息ID,通知对象)
//把Notification传递给NotificationManager
mNotificationManager =
(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
}
二、自定义通知视图(指状态栏显示的view自定义)
1、创建一个自定义的消息布局
view.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">
<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>
2、创建自定义Notification
public void
showNotificationCustom()
{
//NF2:设置Notification
mNotification = new Notification(R.drawable.icon,"A customized
notification.",System.currentTimeMillis());
//将使用默认的声音来提醒用户
mNotification.defaults = Notification.DEFAULT_SOUND;
//NF3:加载自定义的view
RemoteViews
contentView = new RemoteViews(getPackageName(),R.layout.view);
//创建RemoteViews
contentView.setImageViewResource(R.id.image, R.drawable.menu_bg);
//加载图标
contentView.setTextViewText(R.id.text, "Hello,this is JC");
//设置文字
mNotification.contentView =
contentView;
//NF4:显示Notification,设置通知的事件消息
Intent notificationIntent = new
Intent(this,BindServiceActivity.class);
PendingIntent contentIntent =
PendingIntent.getActivity(this,0,notificationIntent,0);
mNotification.contentIntent =
contentIntent;
//把Notification传递给NotificationManager
NotificationManager mNotificationManager =
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
mNotificationManager.notify(0,mNotification);
}
三、扩展
1、Notification提供了丰富的手机提示方式:
a)在状态栏(Status
Bar)显示的通知文本提示,如:
notification.tickerText
= "hello";
b)发出提示音,如:
notification.defaults |=
Notification.DEFAULT_SOUND;
notification.sound =
Uri.parse("file:///sdcard/notification/ringer.mp3");
notification.sound =
Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI,
"6");
c)手机振动,如:
notification.defaults |=
Notification.DEFAULT_VIBRATE;
long[] vibrate
= {0,100,200,300};
notification.vibrate =
vibrate;
d)LED灯闪烁,如:
notification.defaults |=
Notification.DEFAULT_LIGHTS;
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |=
Notification.FLAG_SHOW_LIGHTS;
4)发送通知:
private static final int ID_NOTIFICATION =
1;
mNotificationManager.notify(ID_NOTIFICATION,
notification);
2. 通知的更新
如果需要更新一个通知,只需要在设置好notification之后,再调用setLatestEventInfo(),然后重新发送一次通知即可,即再次调用notify()。
3. 自定义通知视图
这部分可以参考官方文档,讲的很详细了。
AndroidSDK: docs/guide/topics/ui/notifiers/notifications.html
参考文档:
AndroidSDK1.5 : docs/guide/topics/ui/notifiers/notifications.html
4、只有Activity和Serviece可以开启通知,其他的组件包括广播接收器并不能直接开启。如果需要对系统广播进行消息提示的话,
则需要在广播接收器中转移到Activity或者Service中,由他们开启通知。
参考文档:http://blog.csdn.net/ddna/article/details/5122083
PS:目前有两个疑问,正在继续探索:
1、如何显示在running中?
答:Bind的Service不能显示在running中,虽验证过,但不能完全确定
2、activity强制关闭后,notification为何还在?
答:原来Service的Ondestroy重载函数里未加:
mNotificationManager.cancel(NOTIFICATION_ID);//销毁通知
Notification Manager 总结,布布扣,bubuko.com
原文:http://www.cnblogs.com/clarence/p/3571886.html