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. 理解public,protected 以及 private

    经常看到在类中看到public,protected,private以及它们在继承中表示的一些访问范围,很容易搞糊涂.我们首先要明白下面几点. 1.类的一个特征就是封装,public和private作用 ...

  2. linux操作系统cp命令

  3. codeforce 606A - Magic Spheres

    题意:a,b,c三种球,能把俩个一样的球变成另一颜色不一样的球.给你目标x,y,z,问能否经过变化至少达打目标. #include<iostream> #include<stdio. ...

  4. HDU-4678 Mine 博弈SG函数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4678 题意就不说了,太长了... 这个应该算简单博弈吧.先求联通分量,把空白区域边上的数字个数全部求出 ...

  5. Spark的部署方式

    1.Spark的应用程序部署 2.Spark的集群部署

  6. 现代程序设计——homework-01

    1.我的GitHub用户 首先,接触到现代程序设计这门课之后我才正式开始使用GitHub和它的客户端,以前都是去网站看代码.扒样例.我注册的账户名为:hennande.目前该账户中有我的第一份关于ho ...

  7. zzuoj 10409 10409: D.引水工程

    10409: D.引水工程 Time Limit: 2 Sec  Memory Limit: 128 MBSubmit: 96  Solved: 34[Submit][Status][Web Boar ...

  8. Stage3D学习笔记(三):使用GPU绘制一个图片

    首先准备我们需要的图片,尺寸必须是2的幂数,我修改了一下Starling的图标拿来用: 还是先看看最终效果: 代码是居于上一节的代码进行修改的: package { import com.adobe. ...

  9. 重金悬赏的微软:提交Win8漏洞以及发布Win8应用

    随着Windows 8.1这个饱受诟病的操作系统的推出,微软想一举改变颓势,也只有从用户体验上下手了. 近来,微软针对Windows 8.1的漏洞,推出了三项奖励措施: 1.对于发现关键性漏洞,并且这 ...

  10. WinForm程序启动控制台窗口Console

    本文转载:http://blog.csdn.net/oyi319/article/details/5753311 2.WinForm程序和控制台窗口Console 如果你调试过SharpDevelop ...