一、NotificationManager类对象ide
<1>getSystemService(Context.NOTIFICATION_SERVICE) 获取通知管理对象布局
<2>notify(int id, Notification notification)ui
//定义通知管理对象this
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//定义通知构造器对象code
NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("标题") .setContentText("文本") .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
(1)普通的通知对象
Intent intent1 = new Intent(this,SecondActivity.class); //设置点击通知时打开的窗口 //定义延迟执行Intent的对象 PendingIntent pIntent = PendingIntent.getActivity(this, 1, intent1, PendingIntent.FLAG_ONE_SHOT); //FLAG_ONE_SHOT:通知仅执行一次 builder.setContentIntent(pIntent); manager.notify(0,builder.build());
(2)取消全部通知图片
manager.cancelAll();
(3) 带进度条的通知资源
final NotificationCompat.Builder progressBuilder = new NotificationCompat.Builder(this); progressBuilder.setSmallIcon(R.drawable.ic_launcher) .setContentTitle("Title") .setContentText("据说宋丹丹要上马年春晚"); new Thread(new Runnable() { @Override public void run() { int i; for(i = 0;i <= 100;i+= 5){ //第一个参数: 进度条的最大值,第二个参数:当前进度,第三个参数:是否为不肯定性进度 progressBuilder.setProgress(100, i, false); manager.notify(3, progressBuilder.build()); try { Thread.sleep(500); //每隔0.5秒发送一次通知 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } progressBuilder.setContentText("下载完毕!"); manager.notify(3, progressBuilder.build()); } }).start();
二、NotificationCompat.Builder 通知的构造类get
<1>普统统知it
(1)setSmallIcon(R.drawable.ic_launcher) 设置通知的小图标
(2)setContentTitle("标题")
(3)setContentText("文本内容")
(4)setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE) 设置通知提示
(5)setContentIntent(PendingIntent) 设置通知被点击后的意图
(6)Notification build() 生成通知对象
(7).setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.qq))
<2>大视图通知
(1)setStyle(NotificationCompat.Style) 设置大视图样式
setBigContentTitle("大视图标题")
setSummaryText("大视图的说明")
(2)NotificationCompat.InboxStyle 包含一个列表控件
addLine("message1")
(3)NotificationCompat.BigTextStyle 包含一个大的文本控件
bigText("big text")
(4)NotificationCompat.BigPictureStyle 包含一个在的图片控件
bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.qq))
<3>带进度的通知
(1)setProgress(int max, int progress, boolean indeterminate) 设置当前进度,第三个参数:是否为不肯定进度条
<4>自定义通知
(1)setContent(RemoteViews) 设置自定义的通知内容
(2)RemoteViews(String packageName, int layoutId) 加载一个指定应用下的布局资源文件
(3)setTextViewText(int viewId, "内容") 设置指定TextView控件的内容
(4)setImageViewBitmap(int viewId,Bitmap) 设置ImageView控件显示的图片
(5)setImageViewResource(int viewId,int resid) 设置ImageView控件显示的图片资源