作者:刘昊昱

博客:http://blog.csdn.net/liuhaoyutz

状态栏通知涉及到两个类,一是Notification,它代表一个通知;另一个是NotificationManager,它是用于发送Notification的系统服务。

使用状态栏通知一般有4个步骤:

1、  通过getSystemService()方法获取NotificationManager服务。

2、  创建一个Notification对象,并为其设置各种属性。

3、  为Notification对象设置事件信息。

4、  通过NotificationManager类的notify()方法将通知发送到状态栏。

下面我们来看一个例子,其运行效果如下所示:

主布局文件main.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. <Button
  8. android:id="@+id/button1"
  9. android:text="发送消息"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content" />
  12.  
  13. <Button
  14. android:id="@+id/button2"
  15. android:text="清空消息"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content" />
  18.  
  19. </LinearLayout>

下面看主Activity文件,其内容如下所示:

  1. package com.liuhaoyu;
  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.view.View.OnClickListener;
  11. import android.widget.Button;
  12.  
  13. public class MainActivity extends Activity {
  14. final int NOTIFYID_1 = 123; //第一个通知的ID
  15. final int NOTIFYID_2 = 124; //第二个通知的ID
  16.  
  17. /** Called when the activity is first created. */
  18. @Override
  19. public void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.main);
  22.  
  23. final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  24.  
  25. Button button1 = (Button) findViewById(R.id.button1);
  26. button1.setOnClickListener(new OnClickListener() {
  27. @Override
  28. public void onClick(View v) {
  29.  
  30. Notification notify = new Notification();
  31. notify.icon = R.drawable.image01;
  32. notify.tickerText = "有新通知啦!";
  33. notify.when = System.currentTimeMillis();
  34. notify.defaults = Notification.DEFAULT_ALL;
  35. notify.setLatestEventInfo(MainActivity.this, "通知1", "周六下午5点打篮球", null);
  36. notificationManager.notify(NOTIFYID_1, notify);
  37.  
  38. Notification notify1 = new Notification(R.drawable.image01, "哈哈,又有新通知了", System.currentTimeMillis());
  39. notify1.flags|=Notification.FLAG_AUTO_CANCEL;
  40. Intent intent=new Intent(MainActivity.this,ContentActivity.class);
  41. PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
  42. notify1.setLatestEventInfo(MainActivity.this, "通知2",
  43. "点击查看详情", pendingIntent);
  44. notificationManager.notify(NOTIFYID_2, notify1);
  45. }
  46. });
  47.  
  48. Button button2 = (Button) findViewById(R.id.button2);
  49. button2.setOnClickListener(new OnClickListener() {
  50.  
  51. @Override
  52. public void onClick(View v) {
  53. // notificationManager.cancel(NOTIFYID_1); //清除ID号为常量NOTIFYID_1的通知
  54. notificationManager.cancelAll(); //清除全部通知
  55.  
  56. }
  57. });
  58. }
  59. }

当点击第一个按钮时,发送两个通知,第一个直接显示通知内容,第二个通过启动另外一个Activity 即ContentActivity来显示通知内容。

当点击第二个按钮时,清空所有通知,也可以删除指定ID号的通知。

因为第一个通知设置使用默认声音、默认振动及默认闪光灯,即程序需要访问系统闪光灯资源和振动器,所以我们需要在AndroidManifest.xml文件中声明使用权限,加上如下语句:

  1. <uses-permission android:name="android.permission.FLASHLIGHT"/>
  2. <uses-permission android:name="android.permission.VIBRATE"/>

因为第二个通知要启动另外一个Activity来显示通知内容,所以我们也要在AndroidManifest.xml文件中声明这个Activity,加入如下语句:

  1. <activity
  2. android:label="通知"
  3. android:name=".ContentActivity"
  4. android:theme="@android:style/Theme.Dialog" />

通知2调用的活动ContentActivity比较简单,仅仅是显示一条通知,但注意我们通过在AndroidManifest.xml中的android:theme="@android:style/Theme.Dialog"指定它为对话框的形式。该活动实现如下:

  1. package com.liuhaoyu;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5.  
  6. public class ContentActivity extends Activity {
  7.  
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. // TODO Auto-generated method stub
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.content);
  13. }
  14.  
  15. }

其布局文件内容如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6.  
  7. <TextView
  8. android:id="@+id/textView1"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="周日下午5点打羽毛球"
  12. android:textAppearance="?android:attr/textAppearanceMedium" />
  13.  
  14. </LinearLayout>

Android应用开发学习之状态栏通知的更多相关文章

  1. Android应用开发学习之表格视图

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 本文我们来学习一个使用表格视图的程序,下图是该程序的运行效果: 该程序主Activity文件内容如下: packag ...

  2. 2021年正确的Android逆向开发学习之路

    2021年正确的Android逆向开发学习之路 说明 文章首发于HURUWO的博客小站,本平台做同步备份发布.如有浏览或访问异常或者相关疑问可前往原博客下评论浏览. 原文链接 2021年正确的Andr ...

  3. Android应用开发学习笔记之播放音频

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Android支持常用音视频格式文件的播放,本文我们来学习怎样开发Android应用程序对音视频进行操作. Andr ...

  4. android移动开发学习笔记(二)神奇的Web API

    本次分两个大方向去讲解Web Api,1.如何实现Web Api?2.如何Android端如何调用Web Api?对于Web Api是什么?有什么优缺点?为什么用WebApi而不用Webservice ...

  5. Android成长日记-Noification实现状态栏通知

    Notification可以作为状态栏的通知,实现这个效果需要使用NotificationManager实现控制类,才能实现对这个效果的显示 下面是实现状态栏显示效果的通知: 1. 首先在Layout ...

  6. Android应用开发学习笔记之事件处理

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Android提供的事件处理机制分为两类:一是基于监听的事件处理:二是基于回调的事件处理.对于基于监听的事件处理,主 ...

  7. Android 系统开发学习杂记(转)

    http://blog.csdn.net/shagoo/article/details/6709430 > 开发环境1.安装 Eclipse 和 android-sdk 并解压安装2.Eclip ...

  8. Android应用开发学习之相对布局

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 相对布局RelativeLayout是指按照组件之间的相对位置进行布局,如一个组件在另一个组件的左边.右边.上边或下 ...

  9. Android应用开发学习笔记之AsyncTask

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 在上一篇文章中我们学习了多线程和Handler消息处理机制,如果有计算量比较大的任务,可以创建一个新线程执行计算工作 ...

随机推荐

  1. <php>统计目录数和文件数

    $dirn = 0; //目录数 $filen = 0; //文件数 //用来统计一个目录下的文件和目录的个数 function getdirnum($file) { global $dirn; gl ...

  2. pyqt之倒计时例子

    from PyQt4.Qt import *from PyQt4.QtCore import *from PyQt4.QtGui import *import sysdef main():    a= ...

  3. Maven .m2 setting.xml配置

    settings.xml <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="h ...

  4. 曾经的足迹——对Linux CAN驱动的理解(1)

    在Ti的AM335X系列Cortext-A8芯片中,CAN模块采用D_CAN结构,实质即两路CAN接口. 在此分享一下对基于AM335X的Linux CAN驱动源码的理解.下面来分析它的驱动源码及其工 ...

  5. [Angular 2] Event in deep

    This lesson talks about the benefits of using the parens-based (click) syntax so that Angular 2 can ...

  6. [小技巧] 把虚拟机中的Linux系统安装到U盘中

    出于各种需求,很多用户可能经常会在Windows系统中安装虚拟机,然后在虚拟机中安装Linux系统.使用虚拟机的优点是可以同时使用多个系统,而缺点也是显然的,也就是程序运行效率较差.   而实际上,L ...

  7. setTimeout()和setInterval()小结

    写在前面:在写H5游戏时经常需要使用定时刷新页面实现动画效果,比较常用即setTimeout()以及setInterval() setTimeout 描述 setTimeout(code,millis ...

  8. CSS3 之 box-shadow

    1. css3 box-shadow CSS3的box-shadow属性可以让我们轻松实现图层阴影效果 box-shadow:  inset(可选 默认没有) x-offset    y-offset ...

  9. VCS仿真生成vpd文件(verilog)

    VCS仿真生成vpd文件(verilog) 一.环境与文件 Linux平台  csh环境 VCS 64bit 代码文件请参考<一个简单的Verilog计数器模型> 二.开始仿真 1.com ...

  10. 转载--DEV GridControl 的一些基本操作

    1. 如何解决单击记录整行选中的问题 View->OptionsBehavior->EditorShowMode 设置为:Click 2. 如何新增一条记录 (1).gridView.Ad ...