Notification就是在桌面的状态通知栏。这主要涉及三个主要类:

Notification:设置通知的各个属性。

NotificationManager:负责发送通知和取消通知

Notification.Builder:Notification内之类,创建Notification对象。非常方便的控制所有的flags,同时构建Notification的风格。

主要作用:

1.创建一个状态条图标。

2.在扩展的状态条窗口中显示额外的信息(和启动一个Intent)。

3.闪灯或LED。

4.电话震动。

5.发出听得见的警告声(铃声,保存的声音文件)。

Notification是看不见的程序组件(Broadcast Receiver,Service和不活跃的Activity)警示用户有需要注意的事件发生的最好途径

下面主要介绍这三个类:

一、NotificationManager

这个类是这三个类中最简单的。主要负责将Notification在状态显示出来和取消。主要包括5个函数:void cancel(int id),void cancel(String tag, int id), void cancelAll(),void notify(int id, Notificationnotification),notify(String tag, int id, Notification notification)

看看这五个函数就知道这个类的作用了。但是在初始化对象的时候要注意:

NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

二、Notification

设置这个类主要是设置Notification的相关属性。初始化

Notification n = new Notification();

Notification里面有很多属性下面选择几个常用的介绍一下

icon  这个是设置通知的图标。像QQ的小企鹅

sound  这个是设置来通知时的提示音。

tickerText  设置提示的文字。

vibrate     来通知时振动。

when       设置来通知时的时间

flag     这个很有意思是设置通知在状态栏显示的方式。它的值可以设置为虾米这些值:

FLAG_NO_CLEAR 将flag设置为这个属性那么通知栏的那个清楚按钮就不会出现

FLAG_ONGOING_EVENT 将flag设置为这个属性那么通知就会像QQ一样一直在状态栏显示

DEFAULT_ALL  将所有属性设置为默认

DEFAULT_SOUND  将提示声音设置为默认

DEFAULT_VIBRATE  将震动设置为默认

三、Notification.Builder

这个类一般用于管理Notification,动态的设置Notification的一些属性。即用set来设置。也没啥好说的。

转自 http://blog.csdn.net/zqiang_55/article/details/7032025

StatusBarService:

package com.example.androidstatusbarnotification;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.util.Log; public class StatusBarService extends IntentService { private final static String TAG = "MainActivity"; public StatusBarService() {
super("StatusBarService");
// TODO Auto-generated constructor stub
} @Override
protected void onHandleIntent(Intent intent) {
Log.i(TAG, "开始下载");
showNotification(false);
try {
Thread.sleep(10000);
showNotification(true);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i(TAG, "下载完成");
} private void showNotification(boolean finish) {
Notification notification = new Notification();
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent conIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); if (!finish) {
notification.icon = R.drawable.ic_launcher;
notification.tickerText = "开始下载";
notification.setLatestEventInfo(this, "下载", "正在下载中……", conIntent);
} else {
notification.icon = R.drawable.ic_launcher;
notification.tickerText = "下载完成";
notification.setLatestEventInfo(this, "下载", "程序下载完毕", conIntent);
} notification.defaults = Notification.DEFAULT_SOUND;// 添加声音提示
// 下边的两个方式可以添加音乐
// notification.sound =
// Uri.parse("file:///sdcard/notification/ringer.mp3");
// notification.sound =
// Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
// audioStreamType的值必须AudioManager中的值,代表着响铃的模式
notification.audioStreamType = android.media.AudioManager.ADJUST_LOWER;
manager.notify(R.layout.activity_main, notification);
} }

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <Button
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始下载" /> </LinearLayout>

MainActivity:

package com.example.androidstatusbarnotification;

import android.os.Bundle;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity { private final static String TAG = "MainActivity";
private Button btnStart = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); btnStart = (Button) this.findViewById(R.id.btnStart);
btnStart.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this, StatusBarService.class);
startService(intent);//开始下载服务
}
});
} /* (non-Javadoc)
* @see android.app.Activity#onStart()
*/
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
manager.cancel(R.layout.activity_main);//此处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.main, menu);
return true;
} }

Android之Notification介绍的更多相关文章

  1. Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)

    在android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等. ...

  2. Android发展简单介绍

    Android一词的本义指“机器人”,同一时候也是Google于2007年11月5日宣布的基于Linux平台的开源手机操作系统的名称,该平台由操作系统.中间件.用户界面和应用软件组成,号称是首个为移动 ...

  3. Android 通知栏Notification的整合 全面学习 (一个DEMO让你全然了解它)

    在android的应用层中,涉及到非常多应用框架.比如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架.通知机制,ActionBar框架等等. ...

  4. Android的Notification的简介-android学习之旅(四十一)

    Notification简介 Notification位于手机饿最上面,用于显示手机的各种信息,包括网络状态,电池状态,时间等. 属性方法介绍 代码示例 package peng.liu.test; ...

  5. Android开发——Notification通知的各种Style详解

    本来是想与之前讲解使用Notification通知使用一起写的,查看了资料,觉得有必要将这Style部分单独拿出来讲解 前篇:Android开发——Notification通知的使用及Notifica ...

  6. 【转】 [置顶] Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)

    在Android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等. ...

  7. android之Notification通知

    我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的. pac ...

  8. Android 通知(Notification)

    1.介绍 2.常用属性 3.java后台代码 package com.lucky.test30notification; import android.app.Notification; import ...

  9. Android sdk目录介绍

    android sdk目录介绍 build-tools 各版本SDK编译工具 docs 离线开发者文档Android SDK API参考文档 extras 扩展开发包,如兼容架包. platforms ...

随机推荐

  1. 【转】WIN32 控制台程序

    http://blog.csdn.net/houmin0036/article/details/7702236 win32控制台项目指在32位Windows命令提示符(即所谓的dos)环境下运行的应用 ...

  2. zabbix3.x web设置手册(2)

    在浏览器中输入:http://10.50.32.48/zabbix/setup.php 如下图: 点击Next step: 如上图,右侧全为ok,则点击Next step:若右侧有fail的情况,需要 ...

  3. ABP框架详解(八)动态ApiController的生成和访问机制

    在ABP框架中提供了一套动态生成ApiController的机制(依然支持原生ApiController的使用方式),虽然说是动态生成ApiController但是实际上并没有真正在启动程序的时候生成 ...

  4. IOS 跳转至AppStore的两种方式

    //第一种方法 直接跳转 id+编号 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps: ...

  5. 两种include方式及filter中的dispatcher解析

    两种include方式 我自己写了一个original.jsp,另外有一个includedPage.jsp,我想在original.jsp中把includedPage.jsp引进来有两种方式: 1.& ...

  6. Aoite 系列 目录

    介绍 本项目从2009年孵化(V->Sofire->Aoite),至今已度过5个年头.一直在优化,一直在重构,一直在商用.有十分完整的单元测试用例.可以放心使用. Aoite on 博客园 ...

  7. Unity3d + NGUI 的多分辨率适配

    一.当下移动设备的主流分辨率(数据来自“腾讯分析移动设备屏幕分辨率分析报告”) 1.1 iOS设备的分辨率主要有:   宽 高 宽高比 960 640 1.5 1136 640 1.775 1024 ...

  8. Android手动签名

    在生成release build时可实现自动签名,所谓手动签名,就是在命令行下完成签名,落实到Android Studio里面,就是在terminal里面做. 下面是三个命令,第一个签名,第二个验证, ...

  9. [分享黑科技]纯js突破localstorage存储上线,远程抓取图片,并转码base64保存本地,最终实现整个网站所有静态资源离线到用户手机效果却不依赖浏览器的缓存机制,单页应用最新黑科技

    好久没有写博客了,想到2年前答应要放出源代码的也没放出来,最近终于有空先把纯js实现无限空间大小的本地存储的功能开源了,项目地址https://github.com/xueduany/localsto ...

  10. 我心中的核心组件(可插拔的AOP)~调度组件quartz.net续~任务管理器的开发

    回到目录 对于任务调度来说,越来越多的团队选择了quartz,它在java和.net环境下表现都十分优秀,配置简单,功能强大,时间表达式配置灵活,但在使用时,还是感觉缺点什么,怎么说,你在服务器上安装 ...