---
title: "android 通知栏"
author: "cuixuxucui"
date: "2016-07-01T09:14:53Z"
tags: []
categories: ["编程语言"]
slug: "android"
draft: false
lastmod: "2016-07-01T20:38:19Z"
id: 2308458
url: "https://smartblack.iteye.com/blog/2308458"
---

一、拿到NotificationManager manager= (NotificationManager)getSystemService(Context.NOTIFICATION\_SERVICE);

二、创建Notification notification = new Notification(R.drawable.icon,"This is ticket text",System.currentTimeMillis());

第一个参数用于指定通知图标。第二个参数用于指定通知的ticker内容，当通知刚被创建时，会在状态栏一闪而过。第三个参数指定通知创建时间，当下拉系统状态栏时，这个时间会显示在相应的通知上。

三、指定通知的实际标题和内容。notification.setLatestEventInfo(context,"This is content title","This is content text",null);

四、发出通知manager.notify(1,notification);//第一个参数表示通知id不能重复

五、点击通知跳转到NotificationActivity，使用延时执行的intent，即PendingIntent

Intent intent = new Intent(this,Notification.class);

PendingIntent pi = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG\_CANCEL\_CURRENT);

notification.setLatestEventInfo(context,"This is content title","This is content text",pi);//第四个参数

manager.notify(1,notification);

六、打开NotificationActivity时，要关闭通知栏。

public class NotificationActivity extends Activity{

   protected void onCreate(Bundle savedInstanceState){

      super.onCreate(savedInstanceState);

 setContentView(R.layout.notification\_layout);

 NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION\_SERVICE);

 manager.cancel(1);//nofify中第一个参数使用的id

   }

}

七、为通知加上声音和振动,LED闪烁

可以直接使用通知的默认效果notification.defaults = Notificaion.DEFAULT\_ALL;

Uri soundUri = Uri.fromFile(new File("/system/media/audio/ringtones/Basic\_tone.ogg"));

notification.sound = soundUri;

long\[] vibrates = {0,1000,1000,1000};//静止时长，振动时长，静止时长……

notification.vibrate = vibrate;

notification.ledARGB = Color.GREEN;

notification.ledOnMS = 1000;//亮起时长

notification.ledOffMS = 1000;//暗去时长

notification.flags = Notificaion.FLAG\_SHOW\_LIGHTS;