通知用于在状态栏显示消息,消息到来时以图标方式表示,如下:

如果需要查看消息,可以拖动状态栏到屏幕下方即可查看消息.

1、Layout布局文件:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="46dp"
android:onClick="test1"
android:text="@string/text_notifi" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="94dp"
android:onClick="clearNoti"
android:text="@string/text_clear" /> </RelativeLayout>

2、string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">lession16-notifi</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string> <string name="text_notifi">Notification应用案例</string>
<string name="text_clear">清除通知</string> </resources>

3、MainActivity

package com.example.lession16_notifi;

import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.view.Menu;
import android.view.View; public class MainActivity extends Activity {
private NotificationManager notificationManager; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE); } @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;
} public void test1(View v){
showNotification("来短信了", "5557", "I love you", R.drawable.ic_launcher, R.drawable.ic_launcher);
} public void showNotification(String tickerText,String contentTitle,String contentText,int iconId,int notiId){
//创建一个Notification
Notification notification=new Notification();
//设置通知 消息 图标
notification.icon=iconId;
//设置发出消息的内容
notification.tickerText=tickerText;
//设置发出通知的时间
notification.when=System.currentTimeMillis(); //设置显示通知时的默认的发声、震动、Light效果
notification.defaults=Notification.DEFAULT_VIBRATE;//震动
//Notification notification = new Notification(R.drawable.ic_launcher, "有新的消息", System.currentTimeMillis()); //3步:PendingIntent android系统负责维护
PendingIntent pendingIntent=PendingIntent.getActivity(this, 0, getIntent(), 0);
//4步: 设置更加详细的信息
notification.setLatestEventInfo(this, contentTitle, contentText, pendingIntent);
//5步:使用notificationManager对象的notify方法 显示Notification消息 需要制定 Notification的标识
notificationManager.notify(notiId, notification);
} public void clearNoti(View v){
notificationManager.cancelAll();//清除所有
} }

注:模拟器要实现震动需要加权限:DEFAULT_VIBRATE

Android的状态栏通知(Notification)的更多相关文章

  1. 【Android】状态栏通知Notification、NotificationManager详解(转)

    在Android系统中,发一个状态栏通知还是很方便的.下面我们就来看一下,怎么发送状态栏通知,状态栏通知又有哪些参数可以设置? 首先,发送一个状态栏通知必须用到两个类:  NotificationMa ...

  2. Android 状态栏通知Notification、NotificationManager简介

    Notification(通知)一般用在电话,短信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个通知,这时手从上方滑动状态栏就可以展开并处理这个通知: 在Android系统中, ...

  3. 状态栏通知Notification的简单使用

    今天在学习Notification时同时参考了一些相关的博客,现在结合自身学习实际来总结一下. 在使用手机时,当有未接来电或者短消息时,通常会在手机屏幕上的状态栏上显示.而在Android中有提醒功能 ...

  4. Android 状态栏通知 Notification

    private NotificationManager manager; private Notification.Builder builder; @Override protected void ...

  5. Android 状态栏通知Notification、NotificationManager详解

    http://www.cnblogs.com/onlyinweb/archive/2012/09/03/2668381.html

  6. Android中的通知—Notification 自定义通知

    Android中Notification通知的实现步骤: 1.获取NotificationManager对象NotificationManager的三个公共方法:①cancel(int id) 取消以 ...

  7. Android推送通知Notification

    参考: 1.http://www.cnblogs.com/anrainie/archive/2012/03/07/2383941.html 总结: 代码:

  8. 安卓状态栏通知Status Bar Notification

    安卓系统通知用户三种方式: 1.Toast Notification 2.Dialog Notification 3.Status Bar Notification Status Bar Notifi ...

  9. Android学习总结(十五) ———— Notification(状态栏通知)基本用法

    一.Notification基本概念  Notification是一种具有全局效果的通知,它展示在屏幕的顶端,首先会表现为一个图标的形式,当用户向下滑动的时候,展示出通知具体的内容.我们在用手机的时候 ...

随机推荐

  1. dom01

    事件冒泡:即事件最开始由最具体的元素(文档中嵌套层次最深的那个节点)接收,然后逐级向上传播至最不具体的那个节点(文档). 事件捕获:即不太具体的节点应该更早接收到事件,而最具体的节点最后接收到事件. ...

  2. OCP-1Z0-051-题目解析-第9题

    9. Which statement is true regarding the INTERSECT operator? A. It ignores NULL values. B. Reversing ...

  3. CentOS上部署Apache、MySQL和PHP

    centos上yum安装很方便,下面介绍编译安装的方式. 第一步要在CentOS上安装gcc.g++等开发工具 可以从系统光盘上安装,或者 #yum groupinstall "Develo ...

  4. HTML5学习资源

    http://www.silverlightchina.net/html/HTML_5/study/ 我们一起学:HTML5标签系列教程(一)-video标签 版权声明:本文博客原创文章.博客,未经同 ...

  5. [译]Java 设计模式之抽象工厂

    (文章翻译自Java Design Pattern: Abstract Factory) 抽象工厂模式针对工厂模式增加了抽象层.如果我们使用抽象工厂模式和工厂模式比较的话,很明显抽象工厂模式增加了一个 ...

  6. 在openwrt上编译最简单的一个ipk包文件

    1 什么是opkg Opkg 是一个轻量快速的套件管理系统,目前已成为 Opensource 界嵌入式系统标准.常用于路由.交换机等嵌入式设备中,用来管理软件包的安装升级与下载. opkg updat ...

  7. php中session和cookie

    cookie 每次请求页面的时候进行验证,如果用户信息存储在数据库中,每次都要执行一次数据库查询,给数据库造成多余的负担.cookie可以被修改的,所以安全系数太低. session是存储在服务器端面 ...

  8. iOS基础 - CALayer

    一.CALayer简介 Core Animation是跨平台的,支持iOS环境和Mac OS X环境 凡是支持跨平台的框架,都不能直接使用UIKit框架,因为UIKit框架只能应用在iOS而不能用于M ...

  9. 回调函数 use

    $info["fulltext"] = preg_replace_callback( $search2, function($matches) use ($search, $uni ...

  10. SQLite数据库操作 (原始操作)

    android提供了一个名为SQLiteDatabase的类,该类封装了一些操作数据库的API, 使用该类可以完成对数据进行添加(Create).查询(Retrieve).更新(Update)和删除( ...