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. POJ 1017 Packets

    题意:有一些1×1, 2×2, 3×3, 4×4, 5×5, 6×6的货物,每个货物高度为h,把货物打包,每个包裹里可以装6×6×h,问最少几个包裹. 解法:6×6的直接放进去,5×5的空隙可以用1× ...

  2. POJ 2677 Tour

    题意:双调欧几里得旅行商问题.算法导论15-1题,从最左边的点严格从左走到右再从右走到左回到起点,所有点都要走且只走一次,求最短路径. 解法:定义dp[i][j]表示从i走到j的双调路径,分为两种情况 ...

  3. Spring @Resource、@Autowired、@Qualifier的注解注入及区别

    spring2.5提供了基于注解(Annotation-based)的配置,我们可以通过注解的方式来完成注入依赖.在Java代码中可以使用 @Resource或者@Autowired注解方式来经行注入 ...

  4. CXF之二(CXF发布webService)

    Apache CXF提供了用于方便地构建和开发WebService的可靠基础架构.它允许创建高性能和可扩展的服务,可以部署在Tomcat和基于spring的轻量级容器中,也可以部署在更高级的服务器上, ...

  5. 转载:C++ 虚函数表解析

    目录(?)[+]   转载:http://blog.csdn.net/haoel/article/details/1948051# 前言 C++中 的虚函数的作用主要是实现了多态的机制.关于多态,简而 ...

  6. HDU4738 Caocao's Bridges 无向图的桥

    一眼题:找所有的桥,然后求最小权值 但是有很多坑点 1:如果本来不联通 输出0,(这个坑我知道) 2:但是还有一个坑,就是当整个连通,最小桥的权值是0时,也必须派一个人去,wa了无数遍(还是太年轻) ...

  7. Mongodb操作总结

    1.Mongovue里面可以直接group by ,这个时候一定要注意,group by的任何条件的是 json, 注意当值是Int,非string型的时候,值不要加上双引号 2.注意group by ...

  8. C语言简短程序gcc编译过程

    一.建立一个×.c源文件.这里起名:rocks.c 二.编辑源代码,在c源文件内输入如下代码: #include <stdio.h> int main() { puts("C R ...

  9. 自己安装的几个Eclipse插件

    http://eclipsenotepad.sourceforge.net This plugin has the simple objective to let developers write s ...

  10. 【Linux 命令】Linux系统下强制用户下线——who,pkill

    [日期]2014年11月18日 [平台]Centos 6.5 [工具]who pkill [步骤] 1)准备工作 以root身份登录. 2)执行who命令,查看有哪些用户已登录到当前主机