Notification是显示在手机状态的通知——手机状态栏位于手机屏幕的最上方,那里一般显示了手机当前的网络状态、时间等。Notification所代表的是一种具有全局效果的通知,程序一般通过NotificationManager服务来发送Notification。

提示:NotificationManager是一个重要的系统服务,该API位于android的应用程序框架层,应用程序可通过NotificationManager向系统发送全局通知。

Android 3.0增加Notification.Builder类,通过该类允许开发者更轻松的创建Notification对象。Notification.Builder提供了如下常用方法。

  • setDefaults():设置通知LED灯、音乐、振动等。
  • setAutoCancel():设置点击通知后,状态栏自动删除通知。
  • setContentTitle():方法设置通知标题。
  • setContentText():设置通知内容。
  • setSmallIcon():未通知设置图标。
  • setLargeIcon():未通知设置大图标。
  • setTick():设置通知在状态栏的提示文本。
  • setContentIntent():设置点击通知后将要启动的程序组件对应的PendingIntent。

发送Notification很简单,按如下步骤进行即可。

  1. 调用getSystemService(NOTFICATION_SERVICE)方法获取系统的NotificationManager服务。
  2. 通过构造器创建一个Notification对象。
  3. 为Notification设置各种属性。
  4. 通过NotificationManager发送Notification。

  实例:加薪通知    

本实例示范了如何通过NotificationManager来发送、取消Notification,本实例的界面很简单,只是包含两个普通按钮,分别用于发送Notification和取消Notification。

本示例的界面布局文件如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送Notification"
android:onClick="send"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除Notification"
android:onClick="del"/> </LinearLayout>

该Activity程序的后台代码文件如下:

package org.crazyit.helloworld;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.Menu;
import android.view.View; public class NotificationTest extends Activity { static final int NOTIFICATION_ID =0x123;
NotificationManager nm; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification_test);
//获取系统的NotificationManager服务
nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
}
//为发送通知的按钮的点击事件定义事件处理方法
public void send(View source)
{
//创建一个启动其他Activity的Intent
Intent intent=new Intent(NotificationTest.this,OtherActivity.class);
PendingIntent pi=PendingIntent.getActivity(NotificationTest.this,
0, intent, 0);
Notification notify=new Notification.Builder(this)
//设置打开该通知,该通知自动消失
.setAutoCancel(true)
//设置显示在状态栏的通知提示信息
.setTicker("有新消息")
//设置通知的图标
.setSmallIcon(R.drawable.notify)
//设置通知内同的标题
.setContentTitle("一条新通知")
.setContentText("恭喜您,您加薪了,工资增加20%!")
//设置系统默认的声音、震动
//.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE)
//设置通知的自定义声音
.setSound(Uri.parse("android.resource://org.craztit.ui"+R.raw.msg))
.setWhen(System.currentTimeMillis())
//设置盖通知将要启动程序的Intent
.setContentIntent(pi)
.build();
//发送通知
nm.notify(NOTIFICATION_ID,notify);
}
//为删除通知的按钮的点击事件定义事件处理方法
public void del(View v)
{
//取消通知
nm.cancel(NOTIFICATION_ID);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.notification_test, menu);
return true;
} }

上面的程序中粗体字代码用于为Notification设置各种属性,包括Notification的图标、标题、发送时间等。除此之外,上面的程序还通过setDefaults()方法为Notification设置了声音提示、振动提示、闪光灯等。该属性支持如下属性值。

  • DEFAULT_SOUND:设置使用默认声音。
  • DEFAULT_VIBRATE:设置默认振动。
  • DEFAULT_LIGHTS:设置使用默认闪光灯。
  • ALL:设置使用默认声音、振动、闪光灯。

如果不想使用默认设置,也可以使用如下代码:

//设置自定义声音
setSound("file///sdcard/click.mp3");
//设置自定义振动
setVibrate(new long[]{0,50,100,150});

运行上面的程序,单击程序中“发送Notification”按钮,将可以看到手机屏幕上方出现了一个Notification。将状态栏向下拖动,将可以看到Notification的详情,

上图中Notification还关联了一个Activity:OtherActivity,因此当用户单击“通知”时即可启动OtherActivity——OtherActivity是一个十分简单的程序,故此处不再介绍。

由于上面的程序指定了该Notification要启动OtherActivity,因此一定不要忘记在AndroidManifest.xml文件中声明该Activity。而且上面的程序中还需要访问系统闪光灯、振动器,这也需要在AndroidManifest.xml文件中声明权限。也就是增加如下代码片段即可。

 <!-- 添加操作闪光灯的权限 -->
<uses-permission android:name="android.permission.FLASHLIGHT" />
<!-- 添加操作振动器的权限 -->
<uses-permission android:name="android.permission.VIBRATE" /> <activity
android:name="org.crazyit.helloworld.OtherActivity"
android:label="@string/title_activity_other" >
</activity>

Notification的功能与用法的更多相关文章

  1. 2.6.2 Notification的功能与用法

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...

  2. Notification的功能和用法 加薪通知

    实现通知栏消息的生成和消除 MainActivity.java        public class MainActivity extends Activity   {       static f ...

  3. Android 自学之画廊视图(Gallery)功能和用法

    Gallery与之前讲的Spinner有共同的父类:AbsSpinner,表明Gallery和Spinner都是一个列表框.他们之间的区别在于Spinner显示的是一个垂直的列表框,而Gallery显 ...

  4. 配置 SQL Server Email 发送以及 Job 的 Notification通知功能

    配置 SQL Server Email 发送以及 Job 的 Notification通知功能 在与数据库相关的项目中, 比如像数据库维护, 性能警报, 程序出错警报或通知都会使用到在 SQL Ser ...

  5. 搜索框(SearchView)的功能与用法

    SearchView是搜索框组件,它可以让用户在文本框内输入汉字,并允许通过监听器监控用户输入,当用户用户输入完成后提交搜索按钮时,也通过监听器执行实际的搜索. 使用SearchView时可以使用如下 ...

  6. 数值选择器(NumberPicker)的功能与用法

    数值选择器用于让用户输入数值,用户既可以通过键盘输入数值,也可以通过拖动来选择数值.使用该组件常用如下三个方法. setMinValue(int minVal):设置该组件支持的最小值. setMax ...

  7. 日历视图(CalendarView)组件的功能和用法

    日历视图(CalendarView)可用于显示和选择日期,用户既可选择一个日期,也可通过触摸来滚动日历.如果希望监控该组件的日历改变,可调用CalendarView的setOnDateChangeLi ...

  8. 星级评分条(RatingBar)的功能和用法

    星级评分条与拖动条有相同的父类:AbsSeekBar,因此它们十分相似.实际上星级评分条与拖动条的用法.功能都十分接近:它们都是允许用户通过拖动条来改变进度.RatingBar与SeekBar最大区别 ...

  9. 拖动条(SeekBar)的功能和用法

    拖动条和进度条非常相似,只是进度条采用颜色填充来表明进度完成的程序,而拖动条则通过滑块的位置来标识数值——而且拖动条允许用户拖动滑块来改变值,因而拖动条通常用于对系统的某种数值进行调节,比如调节音量等 ...

随机推荐

  1. HDU-5289<two pointers>

    题意: 求一个数列中存在多少个区间,每个区间内的数的差不超过k; 思路:two_pointers; #include<iostream> #include<cstdio> #i ...

  2. pycharm 安装venv的依赖包

    (venv)$ pip install -r requirements.txt

  3. Android Studio的使用(二)--Debug调试

    使用Android Studio进行Debug调试,这里有一篇比较详细的介绍 http://www.2cto.com/kf/201506/408358.html 故不再重复介绍.

  4. Polipo

    polipo代理服务器简介 HTTP代理polipo的配置与使用:http://wuwei5460.blog.51cto.com/2893867/1407369/

  5. linux权限---【600,644,700,755,711,666,777】 - - 博客频道 - CSDN.NET

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

  6. UILabel的抗压缩、抗拉伸、以及控件的约束简述

    今天来说一说UILabel的约束设置问题 首先主要介绍:Priority(控件约束的优先级).Content Hugging Priority(控件抗拉伸优先级).Content Compressio ...

  7. 子序列和问题 acm

    题目描述 给定一个序列 {a1,a2,…,an},定义从a[l]到a[r]的连续子序列的和为sum[l,r],即sum[l,r]=sigma{ai},l<=i<=r.(1<=l< ...

  8. CentOS 5.8 x64 源码安装 samba-3.6.9

    环境 CentOS 5.8 X64      wget http://www.samba.org/samba/ftp/stable/samba-3.6.9.tar.gz   tar zxvf samb ...

  9. MAP--复杂map结构的构造

    我的关键结构比如 struct{     int a;     int b;     int c; }s: 因为这三个数据是基本信息,可以唯一区别一个设备.拿这样一个数据结构作为索引就能找到每个设备. ...

  10. c3p0、dbcp、tomcat jdbc pool 连接池配置简介及常用数据库的driverClass和驱动包

    [-] DBCP连接池配置 dbcp jar包 c3p0连接池配置 c3p0 jar包 jdbc-pool连接池配置 jdbc-pool jar包 常用数据库的driverClass和jdbcUrl ...