一、概述

     Notification这个部件的功能是在状态栏里显示消息提醒,比如有未读的短信或者是未接的电话,那么状态栏里都会有显示,更或者是从某个应用(比如QQ,酷我音乐等等)里按Home键回到桌面,这时状态栏里也会显示这个应用的图标,这就是Notification。

二、要求

程序主界面上有一个Button按钮,当用户点击这个按钮时状态栏会显示一则通知,当按住状态栏下拉时可以看到这个通知在下拉列表里,此时点击这个通知就跳转到另一个界面(相当于查看这个通知)并且能将这个通知在状态栏里取消。

三、实现

新建工程MyNotice,在/res/layout/main.xml文件里添加一个Button:

 <Button
android:id="@+id/mbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Notice"
/>

完整的main.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" /> <Button
android:id="@+id/mbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Notice"
/> </LinearLayout>

修改后的MyNoticeActivity.java文件

 1 package com.nan.notice;
2
3 import android.app.Activity;
4 import android.app.Notification;
5 import android.app.NotificationManager;
6 import android.app.PendingIntent;
7 import android.content.Intent;
8 import android.os.Bundle;
9 import android.view.View;
10 import android.widget.Button;
11
12
13 public class MyNoticeActivity extends Activity
14 {
15 //通知的编号
16 static final int MYNOTICE = 0;
17
18 //定义各个对象
19 private Button mButton = null;
20 private NotificationManager mNotificationManager = null;
21 private Intent mIntent = null;
22 private Notification mNotification = null;
23 private PendingIntent mPendingIntent = null;
24
25
26 /** Called when the activity is first created. */
27 @Override
28 public void onCreate(Bundle savedInstanceState)
29 {
30 super.onCreate(savedInstanceState);
31 setContentView(R.layout.main);
32
33 //mButton实例化
34 mButton = (Button)findViewById(R.id.mbutton);
35 //mNotificationManager实例化
36 mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
37
38 mIntent = new Intent();
39 //设置要跳转到的Activity
40 mIntent.setClass(MyNoticeActivity.this, Activity2.class);
41 //设置点击下拉状态栏列表里的这个通知时所要显示的Activity
42 mPendingIntent = PendingIntent.getActivity(MyNoticeActivity.this, 0, mIntent, 0);
43 mNotification = new Notification();
44 //设置在通知栏里显示的图标
45 mNotification.icon = R.drawable.ic_launcher;
46 //设置在通知栏里显示的文本
47 mNotification.tickerText = "Button 通知...";
48 //设置通知铃声
49 mNotification.defaults = Notification.DEFAULT_SOUND;
50 //设置在下拉状态栏时所显示的关于这个通知的内容
51 mNotification.setLatestEventInfo(MyNoticeActivity.this, "Button", "Button通知", mPendingIntent);
52 //设置按钮监听
53 mButton.setOnClickListener(new View.OnClickListener()
54 {
55 @Override
56 public void onClick(View v)
57 {
58 // TODO Auto-generated method stub
59 //执行这个通知
60 mNotificationManager.notify(MYNOTICE, mNotification);
61
62 }
63 });
64
65 }
66
67 }

在/src里添加一个名为Activity2.java文件:

 1 package com.nan.notice;
2
3 import android.app.Activity;
4 import android.app.NotificationManager;
5 import android.os.Bundle;
6
7
8 public class Activity2 extends Activity
9 {
10 //与MyNoticeActivity.java中定义的值相同
11 static final int MYNOTICE = 0;
12
13 private NotificationManager mNotificationManager = null;
14
15 /** Called when the activity is first created. */
16 @Override
17 public void onCreate(Bundle savedInstanceState)
18 {
19 super.onCreate(savedInstanceState);
20 setContentView(R.layout.activity2);
21
22 mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
23
24 }
25
26 @Override
27 public void onResume()
28 {
29 super.onResume();
30 //在Activity显示完成后取消在状态栏里的这个通知
31 mNotificationManager.cancel(MYNOTICE);
32 }
33
34 }

在/res/layout里添加一个activity2.xml文件

 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6
7 <TextView
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"
10 android:text="Button Notificition"
11 android:textSize="30px"
12 />
13
14 </LinearLayout>

在AndroidManifest.xml文件里声明多一个activity

<activity
android:name=".Activity2"
>
</activity>

好了,运行程序后,如下

点击按钮后,可以看到状态栏里显示一个消息:

按住状态栏然后下拉,可以看到有一条提示:

点击这条提示,进入到这条提示的内容,同时状态栏里的这个通知也消失了:

要求完成!

Android应用开发基础篇(2)-----Notification(状态栏通知)的更多相关文章

  1. Android应用开发基础篇(1)-----Button

    Android应用开发基础篇(1)-----Button   一.概述        Button,顾名思义就是按钮的意思,它主要的功能是响应用户按下按钮时的动作. 二.应用      新建一个工程, ...

  2. Android应用开发基础篇(3)-----ListView

    一.概述 ListView是一个列表显示控件,它的应用非常广泛,在很多应用程序中都可以看到它的身影,比如来电通,网易新闻等等,特别是QQ.因此非常有必要熟练掌握它. 二.要求 能够利用ListView ...

  3. Android应用开发基础篇(4)-----TabHost(选项卡)

    一.概述 TabHost是一种用来显示标签的组件,不清楚?看一下来电通这个应用就知道了.这个组件用起来与其他组件不太一样,它需要继承TabActivity这个类,还有它的布局文件与我们平时用的也有些不 ...

  4. Android应用开发基础篇(14)-----自定义标题栏

    一.概述 每一个应用程序默认的标题栏(注意与状态栏的区别)只有一行文字(新建工程时的名字),而且颜色.大小等都是固定的,给人的感觉比较单调.但当程序需要美化的时候,那么修改标题栏是就是其中一项内容,虽 ...

  5. Android应用开发基础篇(12)-----Socket通信

    链接地址:http://www.cnblogs.com/lknlfy/archive/2012/03/03/2378669.html 一.概述 网络通信无论在手机还是其他设备上都应用得非常广泛,因此掌 ...

  6. Android应用开发基础篇(12)-----Socket通信(转载)

    转自:http://www.devdiv.com/android_socket_-blog-258060-10594.html 一.概述 网络通信无论在手机还是其他设备上都应用得非常广泛,因此掌握网络 ...

  7. Android应用开发基础篇(16)-----ScaleGestureDetector(缩放手势检测)

    链接地址:http://www.cnblogs.com/lknlfy/archive/2012/03/11/2390274.html 一.概述 ScaleGestureDetector这个类是专门用来 ...

  8. Android应用开发基础篇(15)-----URL(获取指定网址里的图片)

    链接地址:http://www.cnblogs.com/lknlfy/archive/2012/03/10/2389190.html 一.概述 URL,说白了就是一个网络地址(网址),通常一个网址里包 ...

  9. Android应用开发基础篇(13)-----GestureDetector(手势识别)

    链接地址:http://www.cnblogs.com/lknlfy/archive/2012/03/05/2381025.html 一.概述 GestureDetector是一个用于识别手势的类,这 ...

随机推荐

  1. 汇编写下strcpy

    #include <stdio.h> int main() { char *source = "hello world\n"; ] = {}; char *p = de ...

  2. Oracle GoldenGate学习之Goldengate介绍

    Oracle GoldenGate学习之Goldengate介绍 (2012-10-02 17:07:27) 标签: 检查点 数据传输 队列 进程 分类: Goldengate Goldengate介 ...

  3. Ubuntu 12.04 Android2.2源码make** /classes-full-debug.jar Error 41错误解决

    出现make: *** [out/target/common/obj/APPS/CMParts_intermediates/classes-full-debug.jar] Error 41这样的错误最 ...

  4. SDN 编程语言 p4(SDN programming language P4)

    行业趋势,SND是未来. P4 是未来. SDN is inevitably, and P4 is inevitably. P4 = Programming Protocol-Independent ...

  5. Eclipse无法识别(手机)设备的解决方案

    遇到问题 开始学习android一个多月了,用Eclipse开发,用android手机调试.之前一直好好的,突然Eclipse无法识别手机设备了.纠结了好久,找了各种解决方法,弄了一晚上终于解决问题了 ...

  6. Android 教你打造炫酷的ViewPagerIndicator 不仅仅是高仿MIUI

    1.概述 哈,今天给大家带来一个ViewPagerIndicator的制作,相信大家在做tabIndicator的时候,大多数人都用过 TabPageIndicator,并且很多知名APP都使用过这个 ...

  7. Nio Server

    package org.fxc.nio.server; import java.io.FileInputStream; import java.io.IOException; import java. ...

  8. SQL Server JDBC驱动中sqljdbc和sqljdbc4区别

    为了支持向后兼容以及可能的升级方案,JDBC Driver 2.0 在每个安装包中都包括 2 个 JAR 类库:sqljdbc.jar 和 sqljdbc4.jar. qljdbc.jar 类库提供对 ...

  9. ##DAY15——UICollectionView

    DAY15——UICollectionView 创建UICollectionView //创建一个布局对象,采用系统布局类UICollectionViewFlowLayout UICollection ...

  10. SQL Server存储过程和游标有关实例以及相关网址

    内含游标的存储过程实例 第一种写法 GO BEGIN IF (object_id('PT_FAULT_REPORT', 'P') is not null) drop proc PT_FAULT_REP ...