实现通知栏消息的生成和消除

  1. MainActivity.java  
  2.     
  3. public class MainActivity extends Activity  
  4. {  
  5.     static final int NOTIFICATION_ID = 0x123;  
  6.     NotificationManager nm;  
  7.     @Override  
  8.     public void onCreate(Bundle savedInstanceState)  
  9.     {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.main);  
  12.         // 获取系统的NotificationManager服务  
  13.         nm = (NotificationManager)  
  14.                 getSystemService(NOTIFICATION_SERVICE);  
  15.     }  
  16.     // 为发送通知的按钮的点击事件定义事件处理方法  
  17.     public void send(View source)  
  18.     {  
  19.         // 创建一个启动其他Activity的Intent  
  20.         Intent intent = new Intent(MainActivity.this  
  21.                 , OtherActivity.class);  
  22.         PendingIntent pi = PendingIntent.getActivity(  
  23.                 MainActivity.this, 0, intent, 0);  
  24.         Notification notify = new Notification.Builder(this)  
  25.                 // 设置打开该通知,该通知自动消失  
  26.                 .setAutoCancel(true)  
  27.                 // 设置显示在状态栏的通知提示信息  
  28.                 .setTicker("有新消息")  
  29.                 // 设置通知的图标  
  30.                 .setSmallIcon(R.drawable.notify)  
  31.                 // 设置通知内容的标题  
  32.                 .setContentTitle("一条新通知")  
  33.                 // 设置通知内容  
  34.                 .setContentText("恭喜你,您加薪了,工资增加20%!")  
  35.                 // 设置使用系统默认的声音、默认LED灯  
  36.                 // .setDefaults(Notification.DEFAULT_SOUND  
  37.                 // |Notification.DEFAULT_LIGHTS)  
  38.                 // 设置通知的自定义声音  
  39.                 .setSound(Uri.parse("android.resource://org.crazyit.ui/"  
  40.                         + R.raw.msg))  
  41.                 .setWhen(System.currentTimeMillis())  
  42.                 // 设改通知将要启动程序的Intent  
  43.                 .setContentIntent(pi)  // ①  
  44.                 .build();  
  45.         // 发送通知  
  46.         nm.notify(NOTIFICATION_ID, notify);  
  47.     }  
  48.     // 为删除通知的按钮的点击事件定义事件处理方法  
  49.     public void del(View v)  
  50.     {  
  51.         // 取消通知  
  52.         nm.cancel(NOTIFICATION_ID);  
  53.     }  
  54. }  
  55.     
  56.     
  57. OtherActivity  
  58.     
  59. public class OtherActivity extends Activity  
  60. {  
  61.    @Override  
  62.    public void onCreate(Bundle savedInstanceState)  
  63.    {  
  64.       super.onCreate(savedInstanceState);  
  65.       //设置该Activity显示的页面  
  66.       setContentView(R.layout.other);  
  67.    }  
  68. }  
  69.     
  70. XML文件  
  71.     
  72. <?xml version="1.0" encoding="utf-8"?>  
  73. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  74.    android:orientation="horizontal"  
  75.    android:layout_width="match_parent"  
  76.    android:layout_height="match_parent"  
  77.    android:gravity="center_horizontal">  
  78. <Button  
  79.    android:layout_width="wrap_content"   
  80.    android:layout_height="wrap_content"   
  81.    android:text="发送Notification"  
  82.    android:onClick="send"  
  83.    />  
  84. <Button  
  85.    android:layout_width="wrap_content"   
  86.    android:layout_height="wrap_content"   
  87.    android:text="删除Notification"  
  88.    android:onClick="del"  
  89.    />   
  90. </LinearLayout>  
  91.     
  92. other.xml  
  93.     
  94. <?xml version="1.0" encoding="utf-8"?>  
  95. <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  
  96.    android:layout_width="match_parent"  
  97.    android:layout_height="wrap_content"  
  98.    android:gravity="center_horizontal"  
  99.    android:orientation="vertical">  
  100. <!-- 定义一个ImageView -->  
  101. <ImageView  
  102.    android:layout_width="match_parent"  
  103.    android:layout_height="wrap_content"  
  104.    android:src="@drawable/swift"  
  105.    android:layout_gravity="center_horizontal"  
  106.    />  
  107. </LinearLayout>  
  108.     
  109. Menu.xml  
  110.     
  111. <menu xmlns:android="http://schemas.android.com/apk/res/android"  
  112.      xmlns:tools="http://schemas.android.com/tools"  
  113.      tools:context=".MainActivity">  
  114.    <item android:id="@+id/action_settings"  
  115.         android:title="@string/app_name"  
  116.         android:orderInCategory="100"  
  117.         android:showAsAction="never"/>  
  118. </menu>  

效果

Notification的功能和用法 加薪通知的更多相关文章

  1. Notification的功能与用法

    Notification是显示在手机状态的通知——手机状态栏位于手机屏幕的最上方,那里一般显示了手机当前的网络状态.时间等.Notification所代表的是一种具有全局效果的通知,程序一般通过Not ...

  2. 2.6.2 Notification的功能与用法

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...

  3. Javascript自定义事件功能与用法实例分析

    原文地址:https://www.jb51.net/article/127776.htm 本文实例讲述了javascript自定义事件功能与用法.分享给大家供大家参考,具体如下: 概述 自定义事件很难 ...

  4. Android 自学之画廊视图(Gallery)功能和用法

    Gallery与之前讲的Spinner有共同的父类:AbsSpinner,表明Gallery和Spinner都是一个列表框.他们之间的区别在于Spinner显示的是一个垂直的列表框,而Gallery显 ...

  5. 搜索框(SearchView)的功能与用法

    SearchView是搜索框组件,它可以让用户在文本框内输入汉字,并允许通过监听器监控用户输入,当用户用户输入完成后提交搜索按钮时,也通过监听器执行实际的搜索. 使用SearchView时可以使用如下 ...

  6. 数值选择器(NumberPicker)的功能与用法

    数值选择器用于让用户输入数值,用户既可以通过键盘输入数值,也可以通过拖动来选择数值.使用该组件常用如下三个方法. setMinValue(int minVal):设置该组件支持的最小值. setMax ...

  7. 日历视图(CalendarView)组件的功能和用法

    日历视图(CalendarView)可用于显示和选择日期,用户既可选择一个日期,也可通过触摸来滚动日历.如果希望监控该组件的日历改变,可调用CalendarView的setOnDateChangeLi ...

  8. 星级评分条(RatingBar)的功能和用法

    星级评分条与拖动条有相同的父类:AbsSeekBar,因此它们十分相似.实际上星级评分条与拖动条的用法.功能都十分接近:它们都是允许用户通过拖动条来改变进度.RatingBar与SeekBar最大区别 ...

  9. 拖动条(SeekBar)的功能和用法

    拖动条和进度条非常相似,只是进度条采用颜色填充来表明进度完成的程序,而拖动条则通过滑块的位置来标识数值——而且拖动条允许用户拖动滑块来改变值,因而拖动条通常用于对系统的某种数值进行调节,比如调节音量等 ...

随机推荐

  1. mydumper使用

    一.下载安装,打开https://launchpad.net/mydumper #wget https://launchpadlibrarian.net/185032423/mydumper-0.6. ...

  2. AWK用法整理

    printf "1:2::3:::4::::5" | awk -F '[:]+' '{print $4}' [:]+ 表示以1个或多个 :(冒号)作为分隔符 ip  addr  | ...

  3. C#累加器函数Aggregate用法 讲解

    Enumerable.Aggregate 扩展方法在System.Linq命名空间中,是Enumerable类的第一个方法(按字母顺序排名),但确是Enumerable里面相对复杂的方法. MSDN对 ...

  4. Intellij IDEA Debug

    Debug用来追踪代码的运行流程,通常在程序运行过程中出现异常,启用Debug模式可以分析定位异常发生的位置,以及在运行过程中参数的变化.通常我们也可以启用Debug模式来跟踪代码的运行流程去学习三方 ...

  5. Redis 发布与订阅 消息

    基于Redis消息队列-实现短信服务化 1.Redis实现消息队列原理 常用的消息队列有RabbitMQ,ActiveMQ,个人觉得这种消息队列太大太重,本文介绍下基于Redis的轻量级消息队列服务. ...

  6. oracle,PL/SQL新建表

    创建用户 -- Create the user create user U_HQ_JAVA default tablespace USERS temporary tablespace TEMP pro ...

  7. poj1611(并查集简单应用)

    题目链接:http://poj.org/problem?id=1611 思路: 显然是一个并查集的题,很简单,只要将一个group中的学生并在一起,最后遍历1到n-1,看有多少学生的祖先与0的祖先相等 ...

  8. Tomb Raider(暴力模拟)

    Tomb Raider https://hihocoder.com/problemset/problem/1829?sid=1394836 时间限制:1000ms 单点时限:1000ms 内存限制:2 ...

  9. Linux网络编程---htons函数的使用

    htons是将整型变量从主机字节顺序转变成网络字节顺序, 就是整数在地址空间存储方式变为高位字节存放在内存的低地址处. htonl就是把本机字节顺序转化为网络字节顺序所谓网络字节顺序(大尾顺序)就是指 ...

  10. Django的models操作

    一.先看单表操作 增 方式1: models.book.objects.create( Book_name = "aaa", Book_info = "bbb" ...