Android通知系统之Notification

##Notification

定义:

一种能够显示即时信息的控件,该控件显示在标题栏中,拉开后会看到通知的完整样式布局

样式:

1. 普统统知

  • 使用普统统知必须的选项
    三剑客

    • 设置标题:setContentTitle()
    • 设置图标:setSmallIcon()
    • 添加描述:setContentText()
  • 可选项ui

    • setDefaults(系统默认闹铃|系统默认震动)
    • setTicker(设置标题栏显示的内容。通常为contentText)
  • 建立通知的步骤this

    • 定义NotificationComfat.Builder对象
    • 设置必须选项和可选项
  • 发布通知
    NotifacationManager.notify(id,Notification)线程

  • 点击事件code

    • 使用PendingIntent,具体方法以下
      getActivity(Context context,int requestCode,int flags)
      context:获取上下文对象
      requestCode:获取另外一个Activity的返回码
      intent:PendingIntent须要触发的事件,好比跳转Activity
      flags:通知标志位
    • 使用setContentIntent(PendingIntent)将封装好的PendingIntent对

象放入该对象中xml

2. 大视图通知

  • 先建立一个简单通知
  • 使用NotificationCompat.InboxStyle对象设置大视图通知的样式,

代码以下对象


NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("Event tracker") .setContentText("Events received") NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); String[] events = new String[6]; // Sets a title for the Inbox style big view inboxStyle.SetBigContentTitle("Event tracker details:"); ... // Moves events into the big view for (int i=0; i < events.length; i++) { inboxStyle.addLine(events[i]); } // Moves the big view style object into the notification object. mBuilder.setStyle(inBoxStyle);

3. 进度条通知

  • 建立一个普统统知
  • 开启一个线程,在改线程中使用setProgress(进度条的最大值,当前进度值,false),在notify方法即时通知进度条,代码以下
    mBuilder.setProgress(100,incr,false); mNotifyManager.notify(0,mBuilder.build());

4. 自定义通知

  • 建立一个普统统知
  • 建立一个自定义布局
  • 建立一个RemoteViews对象,将xml布局加载到对象中,代码以下事件

    RemoteView content =new RemoteViews(getPackageName(),R.layout.item);
  • 使用setContent(content)方法,将RemoteViews对象加载到普统统知对象中
相关文章
相关标签/搜索