Status Bar Notifications(StatusBarNotification)

简介:

 

以下,结合官方文档来学习,我本人英语一般,顺便带着熟悉下嘛,吼吼

 

 

创建Notification的方式:

1.Notification

2.NotificationManager

官方文档创建如下:

To create a status bar notification:

  1. Get a reference to the NotificationManager: 
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
  2. Instantiate the Notification: 
    int icon = R.drawable.notification_icon;
    CharSequence tickerText = "Hello";
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);
  3. Define the Notification's expanded message and Intent: 
    Context context = getApplicationContext();
    CharSequence contentTitle = "My notification";
    CharSequence contentText = "Hello World!";
    Intent notificationIntent = new Intent(this, MyClass.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
  4. Pass the Notification to the NotificationManager: 
    private static final int HELLO_ID = 1;

    mNotificationManager.notify(HELLO_ID, notification);
    That's it. Your user has now been notified.

需要注意的是:如果要在通知栏中通知不同类型的消息时可以用IntentService来处理异步请求哦。。吼吼。。,使用服务后千万记得要在全局文件中注册服务,不然报错。

如上官方文档中创建的通知,当通知消息完成后可以再在此处设置播放声音或振动或显示一条短消息,如下:

//以下是下载完毕后的某些操作:如,,,,播放声音、显示一条消息或振动或闪烁灯

//notification.sound = notification.DEFAULT_SOUND;

//notification.defaults = Notification.DEFAULT_ALL;

官方文档如下:

 

Adding a sound

You can alert the user with the default notification sound (which is defined by the user) or with a sound specified by your application.

To use the user's default sound, add "DEFAULT_SOUND" to the defaults field:

notification.defaults |= Notification.DEFAULT_SOUND;

To use a different sound with your notifications, pass a Uri reference to the sound field. The following example uses a known audio file saved to the device SD card:

notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");

In the next example, the audio file is chosen from the internal MediaStore's ContentProvider:

notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");

In this case, the exact ID of the media file ("6") is known and appended to the content Uri. If you don't know the exact ID, you must query all the media available in the MediaStore with a ContentResolver. See the Content Providers documentation for more information on using a ContentResolver.

If you want the sound to continuously repeat until the user responds to the notification or the notification is cancelled, add "FLAG_INSISTENT" to the flags field.

Note: If the defaults field includes "DEFAULT_SOUND", then the default sound overrides any sound defined by the sound field.

Adding vibration

You can alert the user with the the default vibration pattern or with a vibration pattern defined by your application.

To use the default pattern, add "DEFAULT_VIBRATE" to the defaults field:

notification.defaults |= Notification.DEFAULT_VIBRATE;

To define your own vibration pattern, pass an array of long values to the vibrate field:

long[] vibrate = {0,100,200,300};

notification.vibrate = vibrate;

The long array defines the alternating pattern for the length of vibration off and on (in milliseconds). The first value is how long to wait (off) before beginning, the second value is the length of the first vibration, the third is the next length off, and so on. The pattern can be as long as you like, but it can't be set to repeat.

Note: If the defaults field includes "DEFAULT_VIBRATE", then the default vibration overrides any vibration defined by the vibrate field.

Adding flashing lights

To alert the user by flashing LED lights, you can implement the default light pattern (if available), or define your own color and pattern for the lights.

To use the default light setting, add "DEFAULT_LIGHTS" to the defaults field:

notification.defaults |= Notification.DEFAULT_LIGHTS;

To define your own color and pattern, define a value for the ledARGB field (for the color), the ledOffMS field (length of time, in milliseconds, to keep the light off), the ledOnMS (length of time, in milliseconds, to keep the light on), and also add "FLAG_SHOW_LIGHTS" to the flags field:

notification.ledARGB = 0xff00ff00;

notification.ledOnMS = 300;

notification.ledOffMS = 1000;

notification.flags |= Notification.FLAG_SHOW_LIGHTS;

In this example, the green light repeatedly flashes on for 300 milliseconds and turns off for one second. Not every color in the spectrum is supported by the device LEDs, and not every device supports the same colors, so the hardware estimates to the best of its ability. Green is the most common notification color.

      本文转自华华世界 51CTO博客,原文链接:http://blog.51cto.com/mzh3344258/733369,如需转载请自行联系原作者

相关文章
|
XML Java Android开发
15. 【Android教程】按钮 Button/ImageButton
15. 【Android教程】按钮 Button/ImageButton
455 2
|
存储 移动开发 前端开发
【Web 前端】H5新特性有哪些?
【4月更文挑战第22天】【Web 前端】H5新特性有哪些?
|
运维 Java 程序员
一个合格的程序员,需要哪些必备技能?
对于一个Java开发来说,编程技能毋庸置疑是很重要的。 但是,除了基本的编程开发能力,其他方面的能力也是体现一个程序员的能力的很重要因素。 比如,问题排查能力、线上运维能力、项目管理能力、协调沟通能力等。 本文,主要来简单介绍一下,作为一个合格的Java开发,除了自身技术成长之外,还有哪些方面可以提升。 类开发技能 第一类,并不是纯coding技能,但是也和开发相关,我称之为类开发技能。 Linux系统 很多人的开发机器是windows,所以平时也基本都是图形化开发界面。但是,这并不意味着你就不需要基本的Linux技巧。 因为,你开发出来的应用可能部署在一台Linux机器上,很
873 0
|
JSON 搜索推荐 前端开发
DingTalk「开发者说」- 酷应用开发之卡片开发和能力套件开放
本篇主要讲解钉钉酷应用中卡片的构造、接口和最佳实践,以及卡片未来的规划和能力套件开放。适用对象:产品经理和有技术背景的开发工程师。
DingTalk「开发者说」- 酷应用开发之卡片开发和能力套件开放
|
机器学习/深度学习 存储 JSON
6款Python可视化工具,总有一款适合你!
6款Python可视化工具,各有所长,适用于不同的场景,但是,它们都有一个共同点:强大且好用!下面就开始正文吧!
6款Python可视化工具,总有一款适合你!
|
机器学习/深度学习
MPNN:消息传递神经网络
MPNN:消息传递神经网络
934 0
MPNN:消息传递神经网络
|
程序员 Linux 测试技术
GitHub分享的微软开源计算器项目
GitHub分享的微软开源计算器项目
GitHub分享的微软开源计算器项目
|
安全 测试技术 开发工具
App 不想被“点名”,mPaaS 隐私合规检测为开发者护航数字生态建设
《个人信息保护法》的即将落地,无疑是近年来政策法规及监管标准不断细化深化、监管查处力度不断加大、处置通报常规化的又一里程碑式的具象体现,App 开发企业如若违规将会面临的各类损失也在日益增加。
1029 0
App 不想被“点名”,mPaaS 隐私合规检测为开发者护航数字生态建设
|
测试技术 开发者
测试工程师必备基础知识:软件质量模型概述
测试工程师必备基础知识:软件质量模型概述
1075 0
conda channels
conda channels
1267 0

热门文章

最新文章