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,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值