状态栏通知Notification、NotificationManager详解

原文连接:http://www.oschina.net/question/234345_40111


Android手机可以向下拖动状态栏来看通知,发送一个状态栏通知必须用到3个类:

  1. NotificationManager → 通知的管理类,负责发通知,清除通知等
  2. Notification → 具体的通知对象
  3. PendingIntent→ 点击通知后负责画面跳转的Intent

NotificationManager

NotificationManager 是一个系统Service,必须通过 getSystemService()方法来获取。

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

NotificationManager发送通知(第一个参数:此Notification的一个唯一标识符,第二个参数:此Notification)

mNotificationManager.notify(NOTIFICATION_ID_BASE, mBaseNotification);

NotificationManager清除通知

mNotificationManager.cancel(NOTIFICATION_ID_BASE);// 通过唯一标识符清除通知
mNotificationManager.cancelAll();// 清除所有通知

Notification

必选参数

  • An icon (通知图标,状态栏和下拉栏都会显示)
  • A title and expanded message (通知的标题和内容)
  • A PendingIntent (点击通知执行页面跳转)

可选参数

  • A ticker-text message (状态栏顶部提示消息)
  • An alert sound (提示音)
  • A vibrate setting (振动)
  • A flashing LED setting (灯光)
  • 等等

更新Notification

先调用Notification的 setLatestEventInfo方法来更新内容,然后再调用NotificationManager的notify()方法即可。

// 第二个参数:下拉状态栏时显示的消息标题
// 第三个参数:下拉状态栏时显示的消息内容
// 第四个参数:点击该通知时执行页面跳转
mBaseNotification.setLatestEventInfo(this, "Title01", "Content01", mPendingIntent);

添加声音

如果要采用默认声音,只要使用default就可以了

mBaseNotification.defaults |= Notification.DEFAULT_SOUND;

如果要使用自定义声音,那么就要用到sound了。如下:

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

上面这种方法,使用的是自己的铃声,如果想用系统自带的铃声,可以这样:

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

默认情况下,通知的声音播放一遍就会结束。 如果你想让声音循环播放,需要为flags参数加上FLAG_INSISTENT。 这样声音会到用户响应才结束,比如下拉状态栏。

mBaseNotification.flags |= Notification.FLAG_INSISTENT;

添加振动

如果是使用默认的振动方式,那么同样也是使用default。

mBaseNotification.defaults |= Notification.DEFAULT_VIBRATE;

当然也可以自己定义振动形式,这边需要用到Long型数组。
// 第一个参数:第一次振动前等待的时间
// 第二个参数:第一次振动的时长
// 第三个参数:第二次振动前等待的时间
// 第四个参数:第二次振动的时长
// 如过数组还有第五个,第六个,···············,等等,依次类推
long[] vir = { 0, 100, 200, 300 };
mMediaNotification.vibrate = vir;
另外还需要注意一点:使用振动器时需要权限,如下:

<uses-permission android:name="android.permission.VIBRATE"></uses-permission>

闪光

使用默认的灯光,如下:

mBaseNotification.defaults |= Notification.DEFAULT_LIGHTS;

自定义闪光:

// 其中ledARGB 表示灯光颜色、ledOnMS 亮持续时间、ledOffMS 暗的时间。 
// 注意:这边的颜色跟设备有关,不是所有的颜色都可以,要看具体设备。
mBaseNotification.ledARGB = 0xff00ff00; 
mBaseNotification.ledOnMS = 300; 
mBaseNotification.ledOffMS = 1000;
mBaseNotification.flags |= Notification.FLAG_SHOW_LIGHTS;
其他设置

flags:

Notification.FLAG_INSISTENT;// 让声音、振动无限循环,直到用户响应

Notification.FLAG_AUTO_CANCEL; // 通知被点击后,自动消失

Notification.FLAG_NO_CLEAR; // 点击'Clear'时,不清除该通知(QQ的通知无法清除,就是用的这个)

PendingIntent

PendingIntent的获取方法:

mPendingIntent = PendingIntent.getActivity(this, 0, getIntent(), 0);

下面附上代码:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/baseNotification"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="BaseNotification" />

    <Button
        android:id="@+id/updateBaseNotification"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="UpdateBaseNotification" />

    <Button
        android:id="@+id/clearBaseNotification"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="ClearBaseNotification" />

    <Button
        android:id="@+id/mediaNotification"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="MediaNotification" />

    <Button
        android:id="@+id/clearMediaNotification"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="ClearMediaNotification" />

    <Button
        android:id="@+id/clearAll"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="ClearAll" />

    <Button
        android:id="@+id/customNotification"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="CustomNotification" />

</LinearLayout>

custom_notification_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:padding="3dp" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="10dp" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:textColor="#000" />

</LinearLayout>

NotificationDemoActivity.java

package com.tianjf;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Audio;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;

public class NotificationDemoActivity extends Activity implements
		OnClickListener {
	private Button mBtnBaseNotification;
	private Button mBtnUpdateBaseNotification;
	private Button mBtnClearBaseNotification;
	private Button mBtnMediaNotification;
	private Button mBtnClearMediaNotification;
	private Button mBtnClearAll;
	private Button mBtnCustomNotification;

	// 通知管理器
	private NotificationManager mNotificationManager;
	// 通知显示内容
	private PendingIntent mPendingIntent;
	// Base Notification ID
	private int NOTIFICATION_ID_BASE = 001;
	// Media Notification ID
	private int NOTIFICATION_ID_MEDIA = 002;
	// Custom Notification ID
	private int NOTIFICATION_ID_CUSTOM = 003;
	// Base Notification
	private Notification mBaseNotification;
	// Media Notification
	private Notification mMediaNotification;
	// Media Notification
	private Notification mCustomNotification;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}

	private void init() {
		mBtnBaseNotification = (Button) findViewById(R.id.baseNotification);
		mBtnUpdateBaseNotification = (Button) findViewById(R.id.updateBaseNotification);
		mBtnClearBaseNotification = (Button) findViewById(R.id.clearBaseNotification);
		mBtnMediaNotification = (Button) findViewById(R.id.mediaNotification);
		mBtnClearMediaNotification = (Button) findViewById(R.id.clearMediaNotification);
		mBtnClearAll = (Button) findViewById(R.id.clearAll);
		mBtnCustomNotification = (Button) findViewById(R.id.customNotification);

		mBtnBaseNotification.setOnClickListener(this);
		mBtnUpdateBaseNotification.setOnClickListener(this);
		mBtnClearBaseNotification.setOnClickListener(this);
		mBtnMediaNotification.setOnClickListener(this);
		mBtnClearMediaNotification.setOnClickListener(this);
		mBtnClearAll.setOnClickListener(this);
		mBtnCustomNotification.setOnClickListener(this);

		mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		mPendingIntent = PendingIntent.getActivity(this, 0, getIntent(), 0);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.baseNotification:
			// 新建通知
			mBaseNotification = new Notification();
			// 设置通知的图标(在状态栏和下拉栏都会显示此图标)
			mBaseNotification.icon = R.drawable.ic_launcher;
			// 设置通知显示的内容(状态栏)
			mBaseNotification.tickerText = "You Clicked Base Notification!";
			// 设置通知的默认提示音,振动,灯光
			// 【|=】的意思是3个都有效,要是【=】的话,只有最后一个有效
			// 如果要全部采用默认值, 用 DEFAULT_ALL
			mBaseNotification.defaults |= Notification.DEFAULT_SOUND;// 默认提示音
			mBaseNotification.defaults |= Notification.DEFAULT_VIBRATE;// 默认振动
			mBaseNotification.defaults |= Notification.DEFAULT_LIGHTS;// 默认灯光
			// 让声音、振动无限循环,直到用户响应
			mBaseNotification.flags |= Notification.FLAG_INSISTENT;
			// 通知被点击后,自动消失
			mBaseNotification.flags |= Notification.FLAG_AUTO_CANCEL;
			// 点击'Clear'时,不清除该通知(QQ的通知无法清除,就是用的这个)
			mBaseNotification.flags |= Notification.FLAG_NO_CLEAR;
			// 第二个参数:下拉状态栏时显示的消息标题
			// 第三个参数:下拉状态栏时显示的消息内容
			// 第四个参数:点击该通知时执行页面跳转
			mBaseNotification.setLatestEventInfo(this, "Title01", "Content01",
					mPendingIntent);
			// 发出状态栏通知
			// The first parameter is the unique ID for the Notification
			// The second parameter is the Notification object.
			mNotificationManager
					.notify(NOTIFICATION_ID_BASE, mBaseNotification);
			break;

		case R.id.updateBaseNotification:
			// 更新通知
			// 比如状态栏提示有一条新短信,还没来得及查看,又来一条新短信的提示。
			// 此时采用更新原来通知的方式比较。
			// (再重新发一个通知也可以,但是这样会造成通知的混乱,而且显示多个通知给用户,对用户也不友好)
			mBaseNotification.setLatestEventInfo(this, "Title02", "Content02",
					mPendingIntent);
			mNotificationManager
					.notify(NOTIFICATION_ID_BASE, mBaseNotification);
			break;

		case R.id.clearBaseNotification:
			// 清除 Base Notification
			mNotificationManager.cancel(NOTIFICATION_ID_BASE);
			break;

		case R.id.mediaNotification:
			mMediaNotification = new Notification();
			mMediaNotification.icon = R.drawable.ic_launcher;
			mMediaNotification.tickerText = "You clicked Media Notification!";
			// 自定义声音
			mMediaNotification.sound = Uri.withAppendedPath(
					Audio.Media.INTERNAL_CONTENT_URI, "6");
			// 通知时发出的振动
			// 第一个参数:第一次振动前等待的时间
			// 第二个参数:第一次振动的时长
			// 第三个参数:第二次振动前等待的时间
			// 第四个参数:第二次振动的时长
			// 如过数组还有第五个,第六个,···············,等等,依次类推
			long[] vir = { 0, 100, 200, 300 };
			mMediaNotification.vibrate = vir;
			mMediaNotification.setLatestEventInfo(this, "Title03", "Content03",
					mPendingIntent);
			mNotificationManager.notify(NOTIFICATION_ID_MEDIA,
					mMediaNotification);
			break;

		case R.id.clearMediaNotification:
			// 清除 Media Notification
			mNotificationManager.cancel(NOTIFICATION_ID_MEDIA);
			break;

		case R.id.clearAll:
			// 清除所有 Notification
			mNotificationManager.cancelAll();
			break;

		case R.id.customNotification:
			// 自定义下拉视图,比如下载软件时,显示的进度条。
			mCustomNotification = new Notification();
			mCustomNotification.icon = R.drawable.ic_launcher;
			mCustomNotification.tickerText = "You clicked Custom Notification!";
			RemoteViews contentView = new RemoteViews(getPackageName(),
					R.layout.custom_notification_view);
			contentView.setImageViewResource(R.id.imageView,
					R.drawable.ic_launcher);
			contentView.setTextViewText(R.id.textView,
					"Hello, this message is in a custom expanded view");
			mCustomNotification.contentView = contentView;
			// 使用自定义下拉视图时,不需要再调用setLatestEventInfo()方法
			// 但是必须定义 contentIntent
			mCustomNotification.contentIntent = mPendingIntent;
			mNotificationManager.notify(NOTIFICATION_ID_CUSTOM,
					mCustomNotification);
			break;

		default:
			break;
		}

	}
}

代码下载:http://download.csdn.net/detail/tianjf0514/4272007



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值