---
title: "Android 之 通知Notification"
author: "1140566087"
date: "2014-03-03T22:30:56Z"
tags: []
categories: ["移动开发"]
slug: "android-notification"
draft: false
lastmod: "2014-01-01T23:15:26Z"
id: 1998123
url: "https://sunzone.iteye.com/blog/1998123"
---

<span style="color:red">**Notification 和 NotificationManager 的基本用法**</span>
<span style="color:darkred">**
1.	用途：用来设置通知；
2.	说明：NotificationManager 为后台运行的服务 ， 用来发送通知；Notification 类表示一个持久性的通知
3.	状态栏和状态条的区别：
a)	状态条：手机屏幕最上方的一个条形状的区域；状态条有很多的信息量，例如：usb连接图标，手机信号图标，电池电量图标，时间图标等等
b)	状态栏：手机从状态条滑下来的可以伸缩的view;  状态栏中一般有两类：
i.	正在进行的程序；
ii.	通知事件
4.	一般Notification 传送的信息：
a)	一个状态条图标
b)	在拉伸状态栏中显示带有大标题，小标题，图标的信息，并且有处理该点击事件；比如调用该程序的入口类；
c)	闪光、led 或者震动；
5.	创建Notification 的步骤：
a)	获取NotificationManager对象：NotificationManager  nm = getSystemService(Service.NOTIFICATION_SERVICE);
b)	设置属性：内容，图标，标题，相应的处理动作；
c)	通过nm.notify(); 方法来执行一个notification快讯；
d)	通过nm.cance(); 方法取消一个快讯；
6.	Notification 类中的常量、字段、方法介绍：
a)	DEFAULT_ALL  		使用所有默认值，声音、震动、闪屏等
b)	DEFAULT_LIGHTS  	使用默认灯光提示；
c)	DEFAULT_SOUNDS   使用默认提示音
d)	DEFAULT_VIBRATE		使用默认手机震动
e)	提示：这些效果常量可以叠加；
7.	相关权限：
a)	手机震动：<uses-permission android:name=”android.permission.VIBRATE”/>
8.	实现：见代码…..
**</span>

```java
package com.example.notification;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Audio;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

	private Button send, cancel;
	private Notification notification; // 通知
	private NotificationManager notificationManager; // 通知系统服务

	/**
	 * 获取布局文件中控件的对象
	 */
	public void init() {
		Log.i("msg", "init()...调用");
		send = (Button) findViewById(R.id.send);
		cancel = (Button) findViewById(R.id.cancel);
	}

	// 程序入口
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		init(); // 初始化

		// 设置监听
		send.setOnClickListener(listener);
		cancel.setOnClickListener(listener);

	}

	/**
	 * 发出通知 --
	 */
	public void sendNotification() {
		// 获取对象
		notificationManager = (NotificationManager) this
				.getSystemService(Service.NOTIFICATION_SERVICE);
		notification = new Notification();
		notification.icon = R.drawable.ic_launcher; // 设置图标，公用图标
		notification.tickerText = "状态条标题，提示标题";
		notification.when = System.currentTimeMillis(); // 当前时间 ，通知时间
		
		// 提示音
		notification.defaults = Notification.DEFAULT_SOUND;
		notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3"); // 播放指定位置音乐
		notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6"); // 系统音乐
		
		// 手机震动 -- 权限： <uses-permission android:name="android.permission.VIBRATE"/>
		notification.defaults = Notification.DEFAULT_VIBRATE;
		long[] vibrate = {0,100,200,300};
		notification.vibrate = vibrate;
		
		// LED 灯闪烁
		notification.defaults = Notification.DEFAULT_LIGHTS;
		notification.ledARGB=0xff00ff00;
		notification.ledOffMS = 1000;
		notification.ledOnMS = 300; // 闪光时间，毫秒
	
		/*
		 * 设置Flag的值：说明
		 * FLAG_AUTO_CANCEL : 通知能被状态按钮清除掉
		 * FLAG_NO_CLEAR : 点击清除按钮，不清除
		 * FLAG_ONGOING_EVENT:  该通知放置在正在运行组中
		 * FLAG_INSISTENT : 是否一直进行，比如播放音乐，直到用户响应
		 */
		notification.flags = Notification.FLAG_ONGOING_EVENT;
		


		Intent intent = new Intent(MainActivity.this, MainActivity.class);
		PendingIntent pi = PendingIntent.getActivity(this, 0, intent,
				PendingIntent.FLAG_UPDATE_CURRENT);

		/*
		 * 功能：显示在拉伸状态栏中的Notification属性，点击后将发送PendingIntent对象 
		 * 1：上下文环境
		 * 2：状态栏中的大标题
		 * 3：状态栏中的小标题；
		 * 4：点击后发送的PendingIntent对象
		 */
		notification.setLatestEventInfo(this, "状态栏标题", "状态栏内容", pi);
		notificationManager.notify(1, notification);
	}

	/**
	 * 取消通知
	 */
	public void cancelNotification() {
		notificationManager.cancel(1);
	}

	// 按钮点击监听器
	private OnClickListener listener = new View.OnClickListener() {

		public void onClick(View v) {

			if (v.getId() == R.id.send) { // 发出通知
				sendNotification();
				return;
			}
			if (v.getId() == R.id.cancel) { // 取消通知
				cancelNotification();
				return;
			}

		}
	};
}


```

<span style="color:red">**下面是布局文件：**</span>

```java
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/send"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="发送通知" />

    <Button
        android:id="@+id/cancel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="取消通知" />


</LinearLayout>
```