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

  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. mongodb基础学习2-基本CRUD

    接着学习一下mongodb的基本的CRUD 先列出基本知识点,再给出相关的例子 增:语法: db.collectionName.insert(document); 1: 增加单篇文档,不指定_id时会 ...

  2. JavaScript eval_r() 函数

    定义和用法 eval_r() 函数可计算某个字符串,并执行其中的的 JavaScript 代码. 语法 eval_r(string) 参数 描述 string 必需.要计算的字符串,其中含有要计算的  ...

  3. Windows自带的端口转发工具netsh使用方法_DOS/BAT

    Windows自带的端口转发工具netsh使用方法_DOS/BAT   作者:用户 来源:互联网 时间:2017-02-22 17:24:30 netsh 端口转发 摘要: 下面的代码在windows ...

  4. pip批量更新安装的包

    ------------------pip批量更新库-------------------- 1)查看过期的库 pip list --outdated  更新单一的库: pip install --u ...

  5. VMware-克隆机,网卡修改

    1.克隆 2.修改网卡eth1为eth0 -persistent-net.rules 3.配置ip vim /etc/sysconfig/network-scripts/ifcfg-etho 4.修改 ...

  6. idea下maven项目打包

    近使用idea运行maven需要打包上传tomcat服务器.但是网上一直零零碎碎的....自己记录一下.以防后面忘记 1.idea中.file →Project Structure(快捷键Ctrl+S ...

  7. MyBatis传入参数

    在MyBatis的select.insert.update.delete这些元素中都提到了parameterType这个属性.MyBatis现在可以使用的parameterType有基本数据类型和Ja ...

  8. 【MySQL报错】ERROR 1558 (HY000): Column count of mysql.user is wrong. Expected 43, found 39.

    原文参考:http://wuzhuti.cn/2348.html 之前在centos6.4系统安装的是自带的mysql 5.1版本,后来升级到了5.6版本,执行以下命令报错 在网上查找原因说说因为升级 ...

  9. The valid characters are defined in RFC 7230 and RFC 3986

    网上大都说什么发送格式与协议定义的不兼容,改tomcat版本或改编码之类的. 本人测试的时候换了个浏览器,不用IE就好了 如果坚持用ie,也有解决方式 @参考文章 成功的方法 在请求地址var url ...

  10. 数字三角形 · Triangle

    从上到下用DP. [抄题]: 给定一个数字三角形,找到从顶部到底部的最小路径和.每一步可以移动到下面一行的相邻数字上. 比如,给出下列数字三角形: [ [2], [3,4], [6,5,7], [4, ...