通知的基本用法一般步骤为:
1.调用Context的getSystemService()方法获得NotificationManager进行管理;//NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
2.实例化Builder;//Builder builder = new Builder(MainActivity.this);
3.设置Builder的参数,即对通知的布局进行设定;
4.设置PendingIntent;
//Intent intent = new Intent(this, NotificationActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
5.用Builder的build()方法创建出Notification的实例;//Notification notification = builder.build();
5.调用NotificationManager的notify()让通知显示出来。//manager.notify(1, notification);
以下为百度查找的资料:
方法或参数介绍:
1.PendingIntent
PendingIntent.getBroadcast(context, requestCode, intent, flags)
PendingIntent.getActivities(context, requestCode, intents, flags)
PendingIntent.getService(context, requestCode, intent, flags)
中的flags属性参数:
FLAG_ONE_SHOT 表示返回的PendingIntent仅能执行一次,执行完后自动取消
FLAG_NO_CREATE 表示如果描述的PendingIntent不存在,并不创建相应的PendingIntent,而是返回NULL
FLAG_CANCEL_CURRENT 表示相应的PendingIntent已经存在,则取消前者,然后创建新的PendingIntent
FLAG_UPDATE_CURRENT 表示更新的PendingIntent
2.notification.flags参数介绍
Notification.FLAG_SHOW_LIGHTS //三色灯提醒,在使用三色灯提醒时候必须加该标志符
Notification.FLAG_ONGOING_EVENT //发起正在运行事件(活动中)
Notification.FLAG_INSISTENT //让声音、振动无限循环,直到用户响应 (取消或者打开)
Notification.FLAG_ONLY_ALERT_ONCE //发起Notification后,铃声和震动均只执行一次
Notification.FLAG_AUTO_CANCEL //用户单击通知后自动消失
Notification.FLAG_NO_CLEAR //只有全部清除时,Notification才会清除 ,不清楚该通知(QQ的通知无法清除,就是用的这个)
Notification.FLAG_FOREGROUND_SERVICE //表示正在运行的服务
3.setVibrate(long[] pattern)
设置震动,需要权限.
4.builder.setOngoing( )
设置为ture,表示它为一个正在进行的通知。简单的说,当为ture时,不可以被侧滑消失。