xml文件:

<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="com.example.notificationdemo.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
>
<Button
android:id="@+id/send_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="send Message"/>
<Button
android:id="@+id/cancel_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="cancel Message"
/>
</LinearLayout>
</RelativeLayout>

源代码:

package com.example.notificationdemo;

import android.app.Activity;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; /**
* Notification是显示在手机状态栏的消息(手机状态栏位于手机最顶端),代表一种全局效果的通知 通知栏的内容:图标,标题,内容,时间,点击后响应
* 如何实现通知栏: 获取notificationManager 显示通知栏:notify(id,notification) 取消通知栏:cancel(id)
* 构造Notification并设置显示内容 通知栏通知可以设置声音提示,指示灯以及震动效果,后两者注意需添加权限
*
*
* @author Administrator
*
*/
public class MainActivity extends Activity implements OnClickListener { private Button send;
private Button cancel;
private NotificationManager manager;
private int NOTIFICATION_ID; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send = (Button) findViewById(R.id.send_btn);
cancel = (Button) findViewById(R.id.cancel_btn);
send.setOnClickListener(this);
cancel.setOnClickListener(this);
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);//系统常用服务
} @Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.send_btn:
sendNotification();
break;
case R.id.cancel_btn:
manager.cancel(NOTIFICATION_ID);//取消发送的通知
break;
}
} /**
* 构造notification并发送到通知栏
*/
private void sendNotification() {
// TODO Auto-generated method stub
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pintent = PendingIntent.getActivity(this, 0, intent, 0);
Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher);// 设置图标
builder.setTicker("hello");// 设置手机状态栏的提示
// 拖拽开手机状态栏提示的内容
builder.setWhen(System.currentTimeMillis());// 设置时间
builder.setContentTitle("通知");// 设置标题
builder.setContentText("内容");// 设置内容
builder.setContentIntent(pintent);// 设置点击之后的响应,意图 /*
* builder.setDefaults(Notification.DEFAULT_SOUND);//设置声音
* builder.setDefaults(Notification.DEFAULT_LIGHTS);//设置指示灯
* builder.setDefaults(Notification.DEFAULT_VIBRATE);//设置震动
*/
builder.setDefaults(Notification.DEFAULT_ALL);
Notification notification = builder.getNotification();// 4.1以下
// builder.build();//4.1及以上
manager.notify(NOTIFICATION_ID, notification);//发送通知到通知栏
} }

Android_Notification的更多相关文章

  1. Android 通知之 Notification

    Notifications | Android Developershttp://developer.android.com/guide/topics/ui/notifiers/notificatio ...

  2. Android Notification使用方法

    1.http://www.cnblogs.com/plokmju/p/android_Notification.html 2.http://blog.csdn.net/vipzjyno1/articl ...

随机推荐

  1. IOS 多线程 NSOperation GCD

    1.NSInvocationOperation NSInvocationOperation * op; NSOperationQueue * que = [[NSOperationQueuealloc ...

  2. 《UNIX环境高级编程 第2版》读书笔记

    CH1-2:基础知识.标准化 1 文件和目录 文件名:不能含/(分隔路径)和null(终止路径),255字符. 目录处理:opendir() readdir() closedir() 更改工作目录:c ...

  3. 题目1096:日期差值(a-b=(a-c)-(b-c))

    http://ac.jobdu.com/problem.php?pid=1096 题目描述: 有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天 输入: 有多组数据,每 ...

  4. Python【基础第一篇】

    一.Python3新特性 编码统一为unicode Python3不支持Twisted,暂时只支持73% 1/2=0.5 print "hello World" 变成 print ...

  5. 在Mac上通过Sublime、Skim编辑LaTeX

    转自:http://painterlin.com/2014/08/10/Using-LaTeX-with-Sublime-and-Skim-for-Mac.html Sublime Text是一款非常 ...

  6. 【现代程序设计】【homework-02】【11061027】

    Q:描述在这么多相似的需求面前, 你怎么维护你的设计 (父类/子类/基类, UML, 设计模式,  或者其它方法) 让整个程序的架构不至于崩溃的? A:由于使用的是面向过程的C语言,所以维护设计这个问 ...

  7. nyoj 44 子串和

    子串和 时间限制:5000 ms  |  内存限制:65535 KB 难度:3   描述 给定一整型数列{a1,a2...,an},找出连续非空子串{ax,ax+1,...,ay},使得该子序列的和最 ...

  8. [OC Foundation框架 - 17] copy语法

    一个对象使用copy或mutableCopy方法可以创建对象的副本 1.copy 需要实现NSCopying协议 创建出来的是不可变副本,如NSString, NSArray, NSDictionar ...

  9. 《数据通信与网络》笔记--QoS

    QoS(Quality of Service)服务质量,是网络的一种安全机制, 是用来解决网络延迟和阻塞等问题的一种技术. 1.QoS参数 衡量服务质量QoS的参数主要包括: 端到端延迟(Delay) ...

  10. 【P3O是什么】P3O认证之项目组合、项目群和项目办公室

    如何区分项目组合办公室与项目群和项目办公室? P3O®(Portfolio,Programmed and Project Offices-项目组合.项目群和项目办公室)是由英国商务部 OGC 于200 ...