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

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

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. Cocos2d-x 3.0final 终结者系列教程12-Vector&amp;map&amp;value

    北京时间昨天下午,温40度.中午12:16我来到了篮球场点.思维1分钟决定开站 转球: 我和另一个3队友半开始, 我手中的球的那一刻我突然火爆球不稳,突然问,淡淡的味道橡胶和烧烤的味道混合. 个腾空跳 ...

  2. Bootstrap的栅格系统

    Bootstrap的栅格系统 上一节:ASP.NET MVC5 + EF6 入门教程 (6) View中的Razor使用 源码下载:点我下载 要做一个完整的系统,除了需要MVC这样的B/S框架及EF这 ...

  3. 【 D3.js 进阶系列 — 5.0 】 直方图

    直方图用于描写叙述概率分布,D3 提供了直方图的布局 Histogram 用于转换数据. 假设有数组 a = [10, 11, 11.5, 12.5, 13, 15, 19, 20 ],如今把10~2 ...

  4. 一个可以直接使用的MsgBox基于form居中API

    可直接复制DialogBox项目(文件夹)到需要的项目中然后直接引用和using CodeProject.Dialog 已修正原作者代码错误的地方,可直接使用,VS2010测试成功 具体可以参考案例T ...

  5. 在ubuntu下使用eclipse来调试ARM程序

    该程序为外部Makefile project,导入到eclipse中来进行编译,之后使用Jlink来进行调试. 这个是因为你在编译的时候没有加-g这个标志,导致没有生成调试文件. 让你指定一个源文件. ...

  6. MVC5模板部署到mono

    VS2013中的MVC5模板部署到mono上的艰辛历程 2014-10-27 09:30 by FuzhePan, 3954 阅读, 46 评论, 收藏, 编辑 部署环境:CentOS7 + Mono ...

  7. html postMessage 创建聊天应用

    应用说明: 这个例子演示如何在门户页面以iframe方式嵌入第三方插件,示例中使用了一个来域名下的应用部件,门户页面通过postMessage来通信.iframe中的聊天部件通过父页面标题内容的闪烁来 ...

  8. 来自 Repository 的一丝线索,Domain Model 再重新设计

    来自 Repository 的一丝线索,Domain Model 再重新设计 写在前面 阅读目录: 疑惑解读 设计窘境 一幅图的灵感 为嘛还是你-Repository 后记 上一篇<No zuo ...

  9. Linux 宿主机安装 MiniGUI

    去MiniGUI官方网站看的时候,很兴奋,安装竟然这么容易. 上帝总是在给你一个苹果之后,赏你一巴掌.我的确是高兴太早了. 首先看一下官网文档的说明步骤: (截取于官方文档) Installing r ...

  10. 取得ASKII码值和汉语拼音

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAXMAAACmCAIAAACnXPjtAAAgAElEQVR4nO2de3wb1YHv56+7e/fe/e ...