Android通知系统之Notification

发布时间:2019-06-13 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Android通知系统之Notification脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

##Notification

定义:

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

样式:

1. 普通通知

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

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

    • setdefaults(系统默认闹铃|系统默认震动)
    • setTicker(设置标题栏显示的内容。一般为contentText)
  • 创建通知的步骤

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

  • 点击事件

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

象放入该对象中

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对象加载到普通通知对象中

脚本宝典总结

以上是脚本宝典为你收集整理的Android通知系统之Notification全部内容,希望文章能够帮你解决Android通知系统之Notification所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。