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

  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. ABAP-长文本处理

  2. 分享 - 27 个机器学习、数学、Python 速查表

      转载自:伯乐在线 - iPytLab,原文链接,侵删 机器学习涉及到的方面非常多.当我开始准备复习这些内容的时候,我找到了许多不同的”速查表”, 这些速查表针对某一主题都罗列出了所有我需要知道的知 ...

  3. 迷你MVVM框架 avalonjs 学习教程13、模板引用

    稍为复杂一点的网站都是多个前端工程师合作而成,因此分工是必需的.简单一点的分工就是一个人负责一个频道,某个页面是由一个人全部做的:但如果涉及到一个页面非常复杂,需要多个人同时动工呢?于是到模板的出场时 ...

  4. mysql 中文编码问题

  5. dp-最长公共子序列

    最长公共子序列(NYOJ36) 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 咱们就不拐弯抹角了,如题,需要你做的就是写一个程序,得出最长公共子序列.tip:最长公 ...

  6. oracle表属性

    1. PCTFREE 要形容一个 BLOCK 的运作,我们可以把一个 BLOCK 想成一个水杯.侍者把水倒入放在我们面前的水杯,要多满呢,我们要求他倒 9 分满好了,这时候 PCTFREE 代表着设定 ...

  7. php71 gdnz

    更新yum库:yum updat yum install epel-release yum install -y gcc gcc-c++ autoconf libjpeg libjpeg-devel ...

  8. Python vars() 函数

    Python vars() 函数  Python 内置函数 描述 vars() 函数返回对象object的属性和属性值的字典对象. 语法 vars() 函数语法: vars([object]) 参数 ...

  9. 132. Palindrome Partitioning II (String; DP)

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  10. httplib:AttributeError: 'module' object has no attribute 'HTTPConnection'

    # -*-coding:gb2312-*- #Function:学习python的httplib模块 import httplib conn = httplib.HTTPConnection(&quo ...